import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:intl/intl.dart'; import 'dart:convert'; import 'dart:io'; import '../main.dart'; import '../classes/alert.dart'; import '../variable/globals.dart' as globals; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: PasswordForgot(), ); } } class PasswordForgot extends StatefulWidget { const PasswordForgot({super.key}); @override _PasswordForgotState createState() => _PasswordForgotState(); } class _PasswordForgotState extends State with ShowAlertDialog { TextEditingController inputEmail = TextEditingController(); convertNulltoEmptyString(var check) { if (check == null) { return ""; } return check; } convertNulltoArray(List check) { if (check == null) { return []; } return check; } String formatDate(String date) { var splitedDate = date.split("/"); var day = splitedDate[0]; var month = splitedDate[1]; var year = splitedDate[2]; return "${year}-${month}-${day}"; } Future _forgotPassword(BuildContext context) async { var email = inputEmail.text; var urlPut = Uri.parse("${globals.api}/password/forgot"); var responsePost = await http.post(urlPut, headers: { HttpHeaders.acceptHeader: 'application/json, text/plain, */*', HttpHeaders.contentTypeHeader: 'application/json' }, body: jsonEncode({ 'email': email, })); print(responsePost.statusCode); if (responsePost.statusCode == 200) { showAlertDialog(context, "Creation", "Un email a été envoyé à ${email}"); return; } final errorMessages = { 400: "Requête mal construite", 406: "Mot de passe incorrect", 404: "Utilisateur inconnu", 403: "Utilisateur désactivé", 410: "Token invalide", 500: "Problème interne du serveur", }; final text = errorMessages[responsePost.statusCode] ?? "Problème d'authentification inconnu"; showAlertDialog(context, "Erreur serveur", text); } @override void initState() { super.initState(); } final _formKey = GlobalKey(); String? _validateField(String? value) { return value!.isEmpty ? 'Champ requis' : null; } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: AppBar( title: Text("Mot de passe oublie"), backgroundColor: Colors.blue, foregroundColor: Colors.white, ), body: Form( key: _formKey, child: SingleChildScrollView( child: Column( children: [ Padding( padding: const EdgeInsets.only( left: 15.0, right: 15.0, top: 15, bottom: 0), //padding: EdgeInsets.symmetric(horizontal: 15), child: TextFormField( controller: inputEmail, validator: (value) => _validateField(value), decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Email', hintText: 'Modifier l\'adresse mail'), ), ), SizedBox( height: 30, ), Container( height: 50, width: 250, decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(20)), child: TextButton( onPressed: () { if (_formKey.currentState!.validate()) { _forgotPassword(context); } }, child: Text( 'Envoyer le mail', style: TextStyle(color: Colors.white, fontSize: 25), ), ), ) ], ), ), )); } }