-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPassReset.dart
117 lines (107 loc) · 3.73 KB
/
PassReset.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_fonts/google_fonts.dart';
class PassReset extends StatefulWidget {
const PassReset({Key? key}) : super(key: key);
@override
State<PassReset> createState() => _PassResetState();
}
class _PassResetState extends State<PassReset> {
final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
bool _isSubmitting = false;
String? _errorMessage;
String? _successMessage;
@override
Future<void> _resetPassword() async {
setState(() {
_isSubmitting = true;
});
try {
await FirebaseAuth.instance.sendPasswordResetEmail(email: _emailController.text.trim());
setState(() {
_successMessage = 'Password reset email sent. Check your inbox.';
_isSubmitting = false;
});
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Password reset email sent. Check your inbox.')));
await Future.delayed(Duration(seconds: 3));
Navigator.pop(context);
} on FirebaseAuthException catch (error) {
setState(() {
_isSubmitting = false;
});
if (error.code == 'user-not-found') {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('User not found. Please check your email.')));
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error.message ?? 'An error occurred. Please try again.')));
}
} catch (error) {
setState(() {
_isSubmitting = false;
});
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error.toString())));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Reset Password',style: GoogleFonts.inter(),),
backgroundColor: Color(0xFF49DED2),
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
},
),
),
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email',
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
return null;
},
),
SizedBox(height: 16.0),
if (_errorMessage != null)
Text(
_errorMessage!,
style: TextStyle(color: Colors.red),
),
ElevatedButton(
onPressed: _isSubmitting
? null
: () {
if (_formKey.currentState!.validate()) {
_resetPassword();
}
},
child: _isSubmitting
? CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
)
: Text('Send Reset Link'),
),
],
),
),
),
),
);
}
}