forked from fayaz07/flutter_firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.dart
300 lines (271 loc) · 9.51 KB
/
verify.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import 'package:flutter/material.dart';
import 'package:flutter_firebase/chat/lets_text.dart';
import 'package:flutter_firebase/providers/phone_auth.dart';
import 'package:flutter_firebase/utils/constants.dart';
import 'package:flutter_firebase/utils/widgets.dart';
import 'package:provider/provider.dart';
class PhoneAuthVerify extends StatefulWidget {
/*
* cardBackgroundColor & logo values will be passed to the constructor
* here we access these params in the _PhoneAuthState using "widget"
*/
final Color cardBackgroundColor = Color(0xFFFCA967);
final String logo = Assets.firebase;
final String appName = "Awesome app";
@override
_PhoneAuthVerifyState createState() => _PhoneAuthVerifyState();
}
class _PhoneAuthVerifyState extends State<PhoneAuthVerify> {
double _height, _width, _fixedPadding;
FocusNode focusNode1 = FocusNode();
FocusNode focusNode2 = FocusNode();
FocusNode focusNode3 = FocusNode();
FocusNode focusNode4 = FocusNode();
FocusNode focusNode5 = FocusNode();
FocusNode focusNode6 = FocusNode();
String code = "";
@override
void initState() {
super.initState();
}
final scaffoldKey =
GlobalKey<ScaffoldState>(debugLabel: "scaffold-verify-phone");
@override
Widget build(BuildContext context) {
// Fetching height & width parameters from the MediaQuery
// _logoPadding will be a constant, scaling it according to device's size
_height = MediaQuery.of(context).size.height;
_width = MediaQuery.of(context).size.width;
_fixedPadding = _height * 0.025;
final phoneAuthDataProvider =
Provider.of<PhoneAuthDataProvider>(context, listen: false);
phoneAuthDataProvider.setMethods(
onStarted: onStarted,
onError: onError,
onFailed: onFailed,
onVerified: onVerified,
onCodeResent: onCodeResent,
onCodeSent: onCodeSent,
onAutoRetrievalTimeout: onAutoRetrievalTimeOut,
);
/*
* Scaffold: Using a Scaffold widget as parent
* SafeArea: As a precaution - wrapping all child descendants in SafeArea, so that even notched phones won't loose data
* Center: As we are just having Card widget - making it to stay in Center would really look good
* SingleChildScrollView: There can be chances arising where
*/
return Scaffold(
key: scaffoldKey,
backgroundColor: Colors.white.withOpacity(0.95),
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: _getBody(),
),
),
),
);
}
/*
* Widget hierarchy ->
* Scaffold -> SafeArea -> Center -> SingleChildScrollView -> Card()
* Card -> FutureBuilder -> Column()
*/
Widget _getBody() => Card(
color: widget.cardBackgroundColor,
elevation: 2.0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0)),
child: SizedBox(
height: _height * 8 / 10,
width: _width * 8 / 10,
child: _getColumnBody(),
),
);
Widget _getColumnBody() => Column(
children: <Widget>[
// Logo: scaling to occupy 2 parts of 10 in the whole height of device
Padding(
padding: EdgeInsets.all(_fixedPadding),
child: PhoneAuthWidgets.getLogo(
logoPath: widget.logo, height: _height * 0.2),
),
// AppName:
Text(widget.appName,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 24.0,
fontWeight: FontWeight.w700)),
SizedBox(height: 20.0),
// Info text
Row(
children: <Widget>[
SizedBox(width: 16.0),
Expanded(
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: 'Please enter the ',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w400)),
TextSpan(
text: 'One Time Password',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.w700)),
TextSpan(
text: ' sent to your mobile',
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.w400),
),
],
),
),
),
SizedBox(width: 16.0),
],
),
SizedBox(height: 16.0),
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
getPinField(key: "1", focusNode: focusNode1),
SizedBox(width: 5.0),
getPinField(key: "2", focusNode: focusNode2),
SizedBox(width: 5.0),
getPinField(key: "3", focusNode: focusNode3),
SizedBox(width: 5.0),
getPinField(key: "4", focusNode: focusNode4),
SizedBox(width: 5.0),
getPinField(key: "5", focusNode: focusNode5),
SizedBox(width: 5.0),
getPinField(key: "6", focusNode: focusNode6),
SizedBox(width: 5.0),
],
),
SizedBox(height: 32.0),
RaisedButton(
elevation: 16.0,
onPressed: signIn,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'VERIFY',
style: TextStyle(
color: widget.cardBackgroundColor, fontSize: 18.0),
),
),
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0)),
)
],
);
_showSnackBar(String text) {
final snackBar = SnackBar(
content: Text('$text'),
duration: Duration(seconds: 2),
);
// if (mounted) Scaffold.of(context).showSnackBar(snackBar);
scaffoldKey.currentState.showSnackBar(snackBar);
}
signIn() {
if (code.length != 6) {
_showSnackBar("Invalid OTP");
}
Provider.of<PhoneAuthDataProvider>(context, listen: false)
.verifyOTPAndLogin(smsCode: code);
}
// This will return pin field - it accepts only single char
Widget getPinField({String key, FocusNode focusNode}) => SizedBox(
height: 40.0,
width: 35.0,
child: TextField(
key: Key(key),
expands: false,
// autofocus: key.contains("1") ? true : false,
autofocus: false,
focusNode: focusNode,
onChanged: (String value) {
if (value.length == 1) {
code += value;
switch (code.length) {
case 1:
FocusScope.of(context).requestFocus(focusNode2);
break;
case 2:
FocusScope.of(context).requestFocus(focusNode3);
break;
case 3:
FocusScope.of(context).requestFocus(focusNode4);
break;
case 4:
FocusScope.of(context).requestFocus(focusNode5);
break;
case 5:
FocusScope.of(context).requestFocus(focusNode6);
break;
default:
FocusScope.of(context).requestFocus(FocusNode());
break;
}
}
},
maxLengthEnforced: false,
textAlign: TextAlign.center,
cursorColor: Colors.white,
keyboardType: TextInputType.number,
style: TextStyle(
fontSize: 20.0, fontWeight: FontWeight.w600, color: Colors.white),
// decoration: InputDecoration(
// contentPadding: const EdgeInsets.only(
// bottom: 10.0, top: 10.0, left: 4.0, right: 4.0),
// focusedBorder: OutlineInputBorder(
// borderRadius: BorderRadius.circular(5.0),
// borderSide:
// BorderSide(color: Colors.blueAccent, width: 2.25)),
// border: OutlineInputBorder(
// borderRadius: BorderRadius.circular(5.0),
// borderSide: BorderSide(color: Colors.white))),
),
);
onStarted() {
_showSnackBar("PhoneAuth started");
// _showSnackBar(phoneAuthDataProvider.message);
}
onCodeSent() {
_showSnackBar("OPT sent");
// _showSnackBar(phoneAuthDataProvider.message);
}
onCodeResent() {
_showSnackBar("OPT resent");
// _showSnackBar(phoneAuthDataProvider.message);
}
onVerified() async {
_showSnackBar(
"${Provider
.of<PhoneAuthDataProvider>(context, listen: false)
.message}");
await Future.delayed(Duration(seconds: 1));
Navigator.of(context)
.push(MaterialPageRoute(builder: (BuildContext context) => LetsChat()));
}
onFailed() {
// _showSnackBar(phoneAuthDataProvider.message);
_showSnackBar("PhoneAuth failed");
}
onError() {
// _showSnackBar(phoneAuthDataProvider.message);
_showSnackBar(
"PhoneAuth error ${Provider
.of<PhoneAuthDataProvider>(context, listen: false)
.message}");
}
onAutoRetrievalTimeOut() {
_showSnackBar("PhoneAuth autoretrieval timeout");
// _showSnackBar(phoneAuthDataProvider.message);
}
}