import 'dart:async'; import 'package:diplomaticquarterapp/config/size_config.dart'; import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart'; import 'package:diplomaticquarterapp/widgets/text/app_texts_widget.dart'; import 'package:flutter/material.dart'; import 'dart:math'; class SMSOTP { final type; final mobileNo; final Function onSuccess; final Function onFailure; final context; int remainingTime = 600; Future timer; static BuildContext _context; static bool _loading; SMSOTP( this.context, this.type, this.mobileNo, this.onSuccess, this.onFailure, ); Map verifyAccountFormValue = { 'digit1': null, 'digit2': null, 'digit3': null, 'digit4': null, }; final focusD1 = FocusNode(); final focusD2 = FocusNode(); final focusD3 = FocusNode(); final focusD4 = FocusNode(); String errorMsg; String displayTime = ''; displayDialog(BuildContext context) async { return showDialog( context: context, builder: (context) { return AlertDialog( title: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ IconButton( icon: Icon(Icons.close), onPressed: () => {Navigator.pop(context), this.onSuccess()}, ) ], ), shape: RoundedRectangleBorder( borderRadius: BorderRadius.all(Radius.circular(10.0))), content: StatefulBuilder(builder: (context, setState) { if (displayTime == '') { startTimer(setState); } return Container( color: Colors.white, height: SizeConfig.realScreenHeight * 0.5, width: SizeConfig.realScreenWidth * 0.8, child: Center( child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ type == 1 ? Image.asset('assets/images/login/103.png') : Image.asset('assets/images/login/104.png'), Padding( padding: EdgeInsets.only(top: 5), child: AppText( TranslationBase.of(context).verificationMessage + ' XXXXXX' + mobileNo .toString() .substring(mobileNo.toString().length - 3), textAlign: TextAlign.center, )), Padding( padding: EdgeInsets.only(top: 20), child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ Container( width: SizeConfig.realScreenWidth * 0.15, child: TextFormField( textInputAction: TextInputAction.next, style: buildTextStyle(), autofocus: true, maxLength: 1, textAlign: TextAlign.center, keyboardType: TextInputType.number, decoration: buildInputDecoration(context), onSaved: (val) {}, validator: validateCodeDigit, onFieldSubmitted: (_) { FocusScope.of(context).requestFocus(focusD2); }, onChanged: (val) { if (val.length == 1) { FocusScope.of(context).requestFocus(focusD2); verifyAccountFormValue['digit1'] = val; checkValue(); } }, ), ), Container( width: SizeConfig.realScreenWidth * 0.15, child: TextFormField( focusNode: focusD2, textInputAction: TextInputAction.next, maxLength: 1, textAlign: TextAlign.center, style: buildTextStyle(), keyboardType: TextInputType.number, decoration: buildInputDecoration(context), onSaved: (val) {}, onFieldSubmitted: (_) { FocusScope.of(context).requestFocus(focusD3); }, onChanged: (val) { if (val.length == 1) { FocusScope.of(context) .requestFocus(focusD3); verifyAccountFormValue['digit2'] = val; checkValue(); } }, validator: validateCodeDigit), ), Container( width: SizeConfig.realScreenWidth * 0.15, child: TextFormField( focusNode: focusD3, textInputAction: TextInputAction.next, maxLength: 1, textAlign: TextAlign.center, style: buildTextStyle(), keyboardType: TextInputType.number, decoration: buildInputDecoration(context), onSaved: (val) {}, onFieldSubmitted: (_) { FocusScope.of(context) .requestFocus(focusD4); }, onChanged: (val) { if (val.length == 1) { FocusScope.of(context) .requestFocus(focusD4); verifyAccountFormValue['digit3'] = val; checkValue(); } }, validator: validateCodeDigit)), Container( width: SizeConfig.realScreenWidth * 0.15, child: TextFormField( focusNode: focusD4, maxLength: 1, textAlign: TextAlign.center, style: buildTextStyle(), keyboardType: TextInputType.number, decoration: buildInputDecoration(context), onFieldSubmitted: (_) { FocusScope.of(context) .requestFocus(focusD4); }, onChanged: (val) { if (val.length == 1) { verifyAccountFormValue['digit4'] = val; checkValue(); } }, validator: validateCodeDigit)), ], ), ), Padding( padding: EdgeInsets.only(top: 10), child: AppText( TranslationBase.of(context).validationMessage + ' ' + displayTime, color: Colors.red, textAlign: TextAlign.center, )) ], )), ); }), ); }); } TextStyle buildTextStyle() { return TextStyle( fontSize: SizeConfig.textMultiplier * 3, ); } InputDecoration buildInputDecoration(BuildContext context) { return InputDecoration( // ts/images/password_icon.png contentPadding: EdgeInsets.only(top: 20, bottom: 20), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(10)), borderSide: BorderSide(color: Colors.black), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(10.0)), borderSide: BorderSide(color: Theme.of(context).primaryColor), ), errorBorder: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(10.0)), borderSide: BorderSide(color: Theme.of(context).errorColor), ), focusedErrorBorder: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(10.0)), borderSide: BorderSide(color: Theme.of(context).errorColor), ), ); } String validateCodeDigit(value) { if (value.isEmpty) { return 'Please enter your Password'; } else if (value.length == 3) { print(value); } else { return null; } } checkValue() { //print(verifyAccountFormValue); if (verifyAccountFormValue['digit1'] != null && verifyAccountFormValue['digit2'] != null && verifyAccountFormValue['digit3'] != null && verifyAccountFormValue['digit4'] != null) { onSuccess(verifyAccountFormValue['digit1'] + verifyAccountFormValue['digit2'] + verifyAccountFormValue['digit3'] + verifyAccountFormValue['digit4']); } } getSecondsAsDigitalClock(int inputSeconds) { var sec_num = int.parse(inputSeconds.toString()); // don't forget the second param var hours = (sec_num / 3600).floor(); var minutes = ((sec_num - hours * 3600) / 60).floor(); var seconds = sec_num - hours * 3600 - minutes * 60; var minutesString = ""; var secondsString = ""; minutesString = minutes < 10 ? "0" + minutes.toString() : minutes.toString(); secondsString = seconds < 10 ? "0" + seconds.toString() : seconds.toString(); return minutesString + ":" + secondsString; } startTimer(setState) { this.remainingTime--; setState(() { displayTime = this.getSecondsAsDigitalClock(this.remainingTime); }); timer = Future.delayed(Duration(seconds: 1), () { if (this.remainingTime > 0) { startTimer(setState); } else { Navigator.pop(context); } }); } static void showLoadingDialog(BuildContext context, bool _loading) async { _context = context; if (_loading == false) { Navigator.of(context).pop(); return; } _loading = true; await showDialog( context: _context, barrierDismissible: false, builder: (BuildContext context) { return SimpleDialog( elevation: 0.0, backgroundColor: Colors.transparent, children: [ Center( child: CircularProgressIndicator( valueColor: AlwaysStoppedAnimation(Colors.black), ), ) ], ); }); } }