177 lines
5.2 KiB
Dart
177 lines
5.2 KiB
Dart
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;
|
|
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
|
import 'package:covas_mobile/gen_l10n/app_localizations.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../locale_provider.dart'; //
|
|
|
|
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<PasswordForgot> with ShowAlertDialog {
|
|
TextEditingController inputEmail = TextEditingController();
|
|
|
|
convertNulltoEmptyString(var check) {
|
|
if (check == null) {
|
|
return "";
|
|
}
|
|
return check;
|
|
}
|
|
|
|
convertNulltoArray(List<String> 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<void> _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) {
|
|
String message =
|
|
AppLocalizations.of(context)?.email_sent ?? "Email has been sent";
|
|
showAlertDialog(
|
|
context,
|
|
AppLocalizations.of(context)?.create ?? "Creation",
|
|
"${message} : ${email}");
|
|
return;
|
|
}
|
|
|
|
final messages = {
|
|
400: AppLocalizations.of(context)?.request_error ??
|
|
"Poorly constructed query",
|
|
406: AppLocalizations.of(context)?.incorrect_password ??
|
|
"Incorrect password",
|
|
404: AppLocalizations.of(context)?.unknown_user ?? "Unknown user",
|
|
403: AppLocalizations.of(context)?.disabled_user ?? "Disabled user",
|
|
410: AppLocalizations.of(context)?.invalid_token ?? "Invalid token",
|
|
500: AppLocalizations.of(context)?.internal_error_server ??
|
|
"Internal error server"
|
|
};
|
|
|
|
final text = messages[responsePost.statusCode] ??
|
|
AppLocalizations.of(context)?.unknown_error_auth ??
|
|
"Unknown error auth";
|
|
showAlertDialog(
|
|
context, AppLocalizations.of(context)?.error ?? "Error", text);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
}
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
|
String? _validateField(String? value) {
|
|
return value!.isEmpty ? 'Champ requis' : null;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.white,
|
|
appBar: AppBar(
|
|
title: Text(AppLocalizations.of(context)?.forgot_password ??
|
|
"Forgot password"),
|
|
backgroundColor: Colors.blue,
|
|
foregroundColor: Colors.white,
|
|
),
|
|
body: Form(
|
|
key: _formKey,
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
children: <Widget>[
|
|
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:
|
|
AppLocalizations.of(context)?.email ?? 'Email',
|
|
hintText: AppLocalizations.of(context)?.enter_email ??
|
|
'Enter the email'),
|
|
),
|
|
),
|
|
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(
|
|
AppLocalizations.of(context)?.send_email ?? 'Send email',
|
|
style: TextStyle(color: Colors.white, fontSize: 25),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
));
|
|
}
|
|
}
|