-
Notifications
You must be signed in to change notification settings - Fork 9
/
forgot_password.dart
148 lines (136 loc) · 4.76 KB
/
forgot_password.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import 'package:ecellapp/screens/forgot_password/cubit/forgot_password_cubit.dart';
import 'package:ecellapp/screens/forgot_password/widgets/otp_field.dart';
import 'package:ecellapp/widgets/ecell_animation.dart';
import 'package:ecellapp/widgets/email_field.dart';
import 'package:ecellapp/widgets/password_field.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../widgets/raisedButton.dart';
class ForgotPasswordScreen extends StatelessWidget {
final TextEditingController emailController = TextEditingController();
final TextEditingController otpController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final TextEditingController confirmPasswordController =
TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocConsumer<ForgotPasswordCubit, ForgotPasswordState>(
listener: (context, state) {
if (state is ForgotPasswordError) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(state.message!)),
);
}
},
builder: (context, state) {
return Stack(
children: [
if (state is ForgotEmailInitial)
_initialForgotPassword(context, state)
else if (state is ForgotLoading)
_buildLoading(context)
else if (state is ForgotOTPInitial)
_enterOTP(context, state)
else if (state is ForgotPasswordError)
_uiUpdateForNetworkError(context, state.state)
else if (state is ForgotResetInitial)
_resetPassword(context, state)
else if (state is ForgotResetSuccess)
_passwordResetSuccess()
else
_initialForgotPassword(context, state)
],
);
},
),
);
}
Widget _uiUpdateForNetworkError(
BuildContext context, ForgotPasswordState state) {
if (state is ForgotEmailInitial) {
return _initialForgotPassword(context, state);
} else if (state is ForgotOTPInitial) {
return _enterOTP(context, state);
} else if (state is ForgotPasswordError) {
return _enterOTP(context, state);
} else if (state is ForgotResetInitial) {
return _resetPassword(context, state);
} else {
return _initialForgotPassword(context, state);
}
}
Widget _initialForgotPassword(
BuildContext context, ForgotPasswordState state) {
return Padding(
padding: const EdgeInsets.all(40.0),
child: Column(
children: [
Text("Enter email"),
EmailField(emailController),
LegacyFlatButton(
onPressed: () {
_sendOTP(context, state);
},
child: Text("Press me")),
],
),
);
}
Widget _buildLoading(BuildContext context) {
double width = MediaQuery.of(context).size.width;
return Center(child: ECellLogoAnimation(size: width / 2));
}
Widget _enterOTP(BuildContext context, ForgotPasswordState state) {
return Padding(
padding: const EdgeInsets.all(40.0),
child: Column(
children: [
Text("Enter otp"),
OTPField(otpController),
LegacyFlatButton(
onPressed: () {
_verifyOtp(context, state);
},
child: Text("Press me")),
],
),
);
}
Widget _passwordResetSuccess() {
return Padding(
padding: const EdgeInsets.all(40.0),
child: Text("success"),
);
}
Widget _resetPassword(BuildContext context, ForgotPasswordState state) {
return Padding(
padding: const EdgeInsets.all(40.0),
child: Column(
children: [
PasswordField(passwordController),
PasswordField(confirmPasswordController),
LegacyFlatButton(
onPressed: () {
_changePassword(context, state);
},
child: Text("change password")),
],
),
);
}
void _sendOTP(BuildContext context, ForgotPasswordState state) {
final cubit = context.read<ForgotPasswordCubit>();
cubit.sendOTP(emailController.text, state);
}
void _verifyOtp(BuildContext context, ForgotPasswordState state) {
final cubit = context.read<ForgotPasswordCubit>();
cubit.checkOTP(otpController.text, state, emailController.text);
}
void _changePassword(BuildContext context, ForgotPasswordState state) {
final cubit = context.read<ForgotPasswordCubit>();
cubit.changePassword(emailController.text, otpController.text,
passwordController.text, confirmPasswordController.text, state);
}
}