Created
December 14, 2020 22:34
-
-
Save ologunB/4ab82817c7b87b47cfdef1882d6a4e84 to your computer and use it in GitHub Desktop.
test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:dio/dio.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:fluttertoast/fluttertoast.dart'; | |
import 'package:provider/provider.dart'; | |
import 'package:thcMobile/components/authPhoneInput.dart'; | |
import 'package:thcMobile/components/headerText.dart'; | |
import 'package:thcMobile/provider/user.dart'; | |
import 'package:thcMobile/screens/auth/components/verifyEnterOtp.dart'; | |
import 'package:thcMobile/screens/auth/signup.dart'; | |
import 'package:thcMobile/screens/dashboard/index.dart'; | |
import './resendCodeTimer.dart'; | |
import 'package:thcMobile/components/authTextInput.dart'; | |
import 'package:thcMobile/helpers/sizeCalculator.dart'; | |
import 'package:thcMobile/components/buttonBlue.dart'; | |
import 'package:thcMobile/components/subText.dart'; | |
import 'package:loading_overlay/loading_overlay.dart'; | |
import 'package:thcMobile/screens/auth/components/registerSuccess.dart'; | |
class VerifyBody extends StatefulWidget { | |
VerifyBody({Key key, @required this.isLogin}) : super(key: key); | |
final bool isLogin; | |
@override | |
_VerifyBodyState createState() => _VerifyBodyState(); | |
} | |
class _VerifyBodyState extends State<VerifyBody> { | |
final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); | |
bool _autoValidate = false; | |
String mobileNumber; | |
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); | |
String smsOTP; | |
String verificationId; | |
bool loading = false; | |
final FirebaseAuth _auth = FirebaseAuth.instance; | |
int _start = 60; | |
Timer _timer; | |
@override | |
void initState() { | |
super.initState(); | |
startTimer(); | |
} | |
@override | |
void dispose() { | |
_timer.cancel(); | |
super.dispose(); | |
} | |
void startTimer() { | |
const oneSec = const Duration(seconds: 1); | |
_timer = new Timer.periodic( | |
oneSec, | |
(Timer timer) => setState( | |
() { | |
if (_start < 1) { | |
timer.cancel(); | |
} else { | |
_start = _start - 1; | |
} | |
}, | |
), | |
); | |
} | |
Future<dynamic> saveMobileNumber(body) async { | |
setState(() { | |
loading = true; | |
}); | |
String _baseUrl = "https://thc2020.herokuapp.com/"; | |
String _id = Provider.of<UserModel>(context, listen: false).id; | |
var responseJson; | |
Response response; | |
Dio dio = new Dio(); | |
response = await dio | |
.patch( | |
_baseUrl + "patient/" + _id, | |
data: body, | |
options: Options( | |
followRedirects: false, | |
validateStatus: (status) { | |
return status < 500; | |
}, | |
headers: { | |
"Content-Type": "application/json", | |
"Connection": 'keep-alive' | |
}), | |
) | |
.catchError((e) { | |
setState(() { | |
loading = false; | |
}); | |
print(e.response.data); | |
var message = ''; | |
if (e.response.data['detail'] != null) { | |
message = e.response.data['detail']; | |
} else { | |
message = | |
e.response.data.toString().substring(1, e.response.data.length - 1); | |
} | |
_scaffoldKey.currentState.showSnackBar(SnackBar( | |
content: Text(message, | |
style: TextStyle( | |
fontSize: sizer(true, 15.0, context), | |
color: Colors.white, | |
// fontWeight: FontWeight.w300, | |
)), | |
// duration: Duration(seconds: 3), | |
)); | |
}); | |
setState(() { | |
loading = false; | |
}); | |
responseJson = response.data; | |
if (response.statusCode != 201) { | |
print(response.statusCode); | |
var message = ''; | |
if (response.data['email'] != null) { | |
message = response.data['email'] | |
.toString() | |
.substring(1, response.data['email'].toString().length - 1); | |
} else { | |
message = response.data.toString(); | |
} | |
_scaffoldKey.currentState.showSnackBar(SnackBar( | |
content: Text(message, | |
style: TextStyle( | |
fontSize: sizer(true, 15.0, context), | |
color: Colors.white, | |
// fontWeight: FontWeight.w300, | |
)), | |
// duration: Duration(seconds: 3), | |
)); | |
} else { | |
Provider.of<UserModel>(context, listen: false) | |
.setId(responseJson['data']['patient_id'].toString()); | |
Provider.of<UserModel>(context, listen: false) | |
.setMainId(responseJson['data']['id'].toString()); | |
Provider.of<UserModel>(context, listen: false) | |
.setToken(responseJson['data']['access_token']); | |
Provider.of<UserModel>(context, listen: false) | |
.setName(responseJson['data']['first_name']); | |
print(responseJson); | |
return true; | |
} | |
} | |
void preVerify() { | |
if (_formKey.currentState.validate()) { | |
_formKey.currentState.save(); | |
verifyFn(); | |
} else { | |
setState(() { | |
_autoValidate = true; | |
}); | |
} | |
} | |
Future<void> signIn() async { | |
final AuthCredential credential = PhoneAuthProvider.getCredential( | |
verificationId: verificationId, smsCode: smsOTP); | |
await FirebaseAuth.instance.signInWithCredential(credential).then((user) { | |
if (user.user != null) { | |
Provider.of<UserModel>(context, listen: false) | |
.setMobileNumber('+234' + this.mobileNumber.substring(1)); | |
// MOBILE_NUMBER = '+234' + this.mobileNumber.substring(1); | |
setState(() { | |
loading = false; | |
}); | |
Fluttertoast.showToast( | |
msg: "verificationCompleted", toastLength: Toast.LENGTH_LONG); | |
print("verificationCompleted"); | |
if (widget.isLogin) { | |
Navigator.of(context).push(MaterialPageRoute( | |
builder: (_) => Dashboard(), | |
)); | |
} else { | |
Navigator.of(context).push(MaterialPageRoute( | |
builder: (_) => | |
SignUp(mobileNumber: '+234' + this.mobileNumber.substring(1)), | |
)); | |
} | |
} else { | |
setState(() { | |
loading = false; | |
}); | |
_scaffoldKey.currentState.showSnackBar(SnackBar( | |
content: Text( | |
'A unexpected error has taken place', | |
), | |
behavior: SnackBarBehavior.floating, | |
backgroundColor: Colors.red, | |
elevation: 0.0, | |
)); | |
} | |
}).catchError((e) { | |
setState(() { | |
loading = false; | |
}); | |
print(e); | |
}); | |
} | |
Future<bool> smsCodeDialog(BuildContext context) { | |
return showDialog( | |
context: context, | |
barrierDismissible: false, | |
builder: (BuildContext context) { | |
return new AlertDialog( | |
title: Text('Enter OTP'), | |
content: TextField( | |
keyboardType: TextInputType.number, | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
color: Color(0xff071232), | |
fontWeight: FontWeight.w500, | |
fontSize: sizer(true, 16.0, context)), | |
cursorColor: Color(0xff245DE8), | |
decoration: InputDecoration( | |
contentPadding: EdgeInsets.symmetric( | |
vertical: sizer(false, 20, context), | |
horizontal: sizer(true, 20, context)), | |
fillColor: Color(0xffF3F4F8), | |
hintText: "OTP", | |
hintStyle: TextStyle( | |
color: Color(0xff828A95), fontSize: sizer(true, 16.0, context)), | |
errorMaxLines: 5, | |
focusedBorder: OutlineInputBorder( | |
borderRadius: BorderRadius.circular(10), | |
borderSide: BorderSide( | |
width: 1.5, | |
color: Color(0xff245DE8), | |
), | |
), | |
border: OutlineInputBorder( | |
borderRadius: BorderRadius.circular(10), | |
borderSide: BorderSide( | |
width: 0, | |
style: BorderStyle.none, | |
), | |
), | |
filled: true, | |
), | |
onChanged: (value) { | |
this.smsOTP = value; | |
}, | |
), | |
contentPadding: EdgeInsets.all(10.0), | |
actions: <Widget>[ | |
new FlatButton( | |
onPressed: () async { | |
if(smsOTP == null){ | |
Fluttertoast.showToast(msg: "Enter the OTP"); | |
return; | |
} | |
if (widget.isLogin) { | |
Fluttertoast.showToast(msg: "Signed In"); | |
Navigator.of(context).push(MaterialPageRoute( | |
builder: (_) => Dashboard(), | |
)); | |
} else { | |
signIn(); | |
} | |
}, | |
child: Text( | |
'Done', | |
style: TextStyle(color: Colors.blue, fontSize: 18), | |
)) | |
], | |
); | |
}, | |
); | |
} | |
Future<void> verifyFn() async { | |
setState(() { | |
loading = true; | |
}); | |
final PhoneCodeSent smsOTPSent = (String verId, [int forceCodeResend]) { | |
this.verificationId = verId; | |
Fluttertoast.showToast(msg: "OTP Sent"); | |
print("smsOTPSent: " + verId); | |
loading = false; | |
setState(() { | |
}); | |
smsCodeDialog(context).then((value) { | |
print('Code sent In'); | |
}); | |
}; | |
try { | |
await _auth.verifyPhoneNumber( | |
phoneNumber: '+234' + this.mobileNumber.substring(1), | |
// PHONE NUMBER TO SEND OTP | |
codeAutoRetrievalTimeout: (String verId) { | |
//Starts the phone number verification process for the given phone number. | |
//Either sends an SMS with a 6 digit code to the phone number | |
//specified, or sign's the user in and [verificationCompleted] is called. | |
this.verificationId = verId; | |
setState(() { | |
loading = false; | |
}); | |
}, | |
codeSent: smsOTPSent, | |
// WHEN CODE SENT THEN WE OPEN DIALOG TO ENTER OTP. | |
timeout: const Duration(seconds: 120), | |
verificationCompleted: (AuthCredential phoneAuthCredential) async { | |
await FirebaseAuth.instance | |
.signInWithCredential(phoneAuthCredential) | |
.then((user) { | |
if (user.user != null) { | |
Navigator.pop(context); | |
Provider.of<UserModel>(context, listen: false) | |
.setMobileNumber('+234' + this.mobileNumber.substring(1)); | |
// MOBILE_NUMBER = '+234' + this.mobileNumber.substring(1); | |
setState(() { | |
loading = false; | |
}); | |
Fluttertoast.showToast( | |
msg: "verificationCompleted", | |
toastLength: Toast.LENGTH_LONG); | |
print("verificationCompleted"); | |
if (widget.isLogin) { | |
Navigator.of(context).push(MaterialPageRoute( | |
builder: (_) => Dashboard(), | |
)); | |
} else { | |
Navigator.of(context).push(MaterialPageRoute( | |
builder: (_) => SignUp( | |
mobileNumber: '+234' + this.mobileNumber.substring(1)), | |
)); | |
} | |
} else { | |
setState(() { | |
loading = false; | |
}); | |
_scaffoldKey.currentState.showSnackBar(SnackBar( | |
content: Text( | |
'A unexpected error has taken place', | |
), | |
behavior: SnackBarBehavior.floating, | |
backgroundColor: Colors.red, | |
elevation: 0.0, | |
)); | |
} | |
}).catchError((e) { | |
setState(() { | |
loading = false; | |
}); | |
print(e); | |
}); | |
}, | |
verificationFailed: (FirebaseAuthException exception) { | |
setState(() { | |
loading = false; | |
}); | |
_scaffoldKey.currentState.showSnackBar(SnackBar( | |
content: Text( | |
exception.message, | |
), | |
behavior: SnackBarBehavior.floating, | |
backgroundColor: Colors.red, | |
elevation: 0.0, | |
)); | |
}); | |
} catch (e) { | |
setState(() { | |
loading = false; | |
}); | |
handleError(e); | |
} | |
} | |
handleError(PlatformException error) { | |
print(error); | |
switch (error.code) { | |
case 'ERROR_INVALID_VERIFICATION_CODE': | |
_scaffoldKey.currentState.showSnackBar(SnackBar( | |
content: Text( | |
'Invalid Code', | |
), | |
behavior: SnackBarBehavior.floating, | |
backgroundColor: Colors.red, | |
elevation: 0.0, | |
)); | |
break; | |
default: | |
_scaffoldKey.currentState.showSnackBar(SnackBar( | |
content: Text( | |
error.message, | |
), | |
behavior: SnackBarBehavior.floating, | |
backgroundColor: Colors.red, | |
elevation: 0.0, | |
)); | |
break; | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
key: _scaffoldKey, | |
body: LoadingOverlay( | |
child: SafeArea( | |
child: verificationId == null || verificationId.length > 0 | |
? Container( | |
width: MediaQuery.of(context).size.width, | |
height: MediaQuery.of(context).size.height, | |
child: new SingleChildScrollView( | |
child: Padding( | |
padding: EdgeInsets.only( | |
top: sizer(false, 100, context), | |
left: 25, | |
right: 25), | |
child: Column( | |
mainAxisSize: MainAxisSize.max, | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
Row( | |
mainAxisSize: MainAxisSize.max, | |
crossAxisAlignment: | |
CrossAxisAlignment.center, | |
mainAxisAlignment: | |
MainAxisAlignment.spaceBetween, | |
children: <Widget>[ | |
Image.asset( | |
'assets/images/brownHand.png', | |
width: 28, | |
height: 28, | |
), | |
Spacer( | |
flex: 1, | |
) | |
], | |
), | |
SizedBox(height: sizer(false, 7, context)), | |
HeaderText(title: 'Hi ' + ','), | |
SizedBox(height: sizer(false, 7, context)), | |
Text('Please verify your account', | |
style: TextStyle( | |
color: Color(0xff071232), | |
fontSize: 28)), | |
SizedBox(height: sizer(false, 41, context)), | |
SubText( | |
title: | |
'Enter otp sent to your mobile number', | |
isCenter: false, | |
), | |
SizedBox(height: sizer(false, 96, context)), | |
new Form( | |
key: _formKey, | |
autovalidate: _autoValidate, | |
child: customForm(), | |
) | |
])))) | |
: VerifyEnterOtpBody( | |
resendToken: () { | |
verifyFn(); | |
}, | |
otp: verificationId, | |
start: _start, | |
isLogin: widget.isLogin, | |
mobileNumber: '+234' + this.mobileNumber.substring(1))), | |
isLoading: loading, | |
)); | |
} | |
Widget customForm() { | |
return Column( | |
crossAxisAlignment: CrossAxisAlignment.center, | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Container( | |
margin: EdgeInsets.only(bottom: sizer(false, 15, context)), | |
child: AuthPhoneInput( | |
hintText: 'Enter Phone Number(0**********)', | |
onChanged: (text) { | |
mobileNumber = text; | |
}), | |
), | |
// ResendCodeTimer(loading: widget.loading, | |
// resendToken: widget.resendToken, | |
// start: widget.start), | |
SizedBox(height: sizer(false, 229, context)), | |
ButtonBlue( | |
onPressed: () { | |
preVerify(); | |
// Navigator.of(context).push( | |
// MaterialPageRoute( | |
// builder: (_) => RegisterSuccess()), | |
// ); | |
}, | |
title: 'SEND OTP'), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment