这里,当我有一个注册按钮时,当我单击注册按钮时,它将在该警报中显示一个警报,我必须输入OTP。计时器为300秒,表示5分钟。当用户单击重新发送时,计时器也需要重置为5分钟,但是在此代码中,我的状态未更新或不知道会发生什么,但是时间未重置,因此我想在用户单击重置按钮时重置时间。我有时间在控制台中,但是没有重置。希望您能理解这个问题。您的帮助可以使我开心。预先谢谢你。
[这里是我尝试过的代码
_resendCode() async {
print('need to resend the code');
print('resending to the email ${_email.text}');
Map resendTokenRes = await _signupRepository.resendToken(
_email.text, 'registration_activation');
if (resendTokenRes['erorr'] != null) {
print('failed to resend');
} else {
_startTime = resendTokenRes['data']['sendToken']['meta']['server_time'];
print('_startTime in resend token');
print(_startTime);
DateTime dateTime = DateTime.parse(_startTime);
_endtime = dateTime.add(new Duration(seconds: 300));
_timerSecs = _endtime.difference(dateTime).inSeconds;
print("==============Start Time================");
print(_startTime);
print(_endtime);
print(_timerSecs);
setState(() {
hasTimerStopped = false;
_timerSecs = _timerSecs;
print('new timer establieshed = $_timerSecs');
});
}
}
Future<void> _timerExpiredDialog() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Time is Expired'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(
'Your time is expired please press reset button to get a new OTP'),
SizedBox(
height: 20,
),
FlatButton(
child: Text("Resend the otp"),
color: colorStyles["primary"],
onPressed: () {
_resendCode();
Navigator.of(context).pop();
},
),
// _resendbutton("Resend the OTP", 5),
],
),
),
);
},
);
}
Widget _timer() {
return Center(
child: CountDownTimer(
secondsRemaining: _timerSecs,
whenTimeExpires: () {
setState(() {
hasTimerStopped = true;
_timerSecs = 0;
if (_timerSecs == 0) {
print("Timer is Expired");
_timerExpiredDialog();
}
});
},
countDownTimerStyle: TextStyle(
fontSize: 25,
color: colorStyles["primary"],
fontWeight: FontWeight.bold),
),
);
}
Future _onDoneVerify(
BuildContext context, SignupViewModel signupVm, String value) async {
setState(() {
passcode = value;
print('passcode : : : : $passcode');
});
if (passcode != '' && passcode.length == 6) {
print("I am in Otp Condition :->");
Map<String, dynamic> otpVerification =
await this._signupRepository.activateUser(_email.text, passcode);
print('Otp Verification : $otpVerification');
if (otpVerification['error'] != null) {
setState(() {
hasError = true;
});
} else {
print('User verfied successfully');
print(otpVerification['data']['registerConfirmation']);
LUser.LoginUser userModel = LUser.LoginUser(
token: otpVerification['data']['registerConfirmation']['token'],
user: LUser.User.fromJson(
otpVerification['data']['registerConfirmation'],
),
);
signupVm.loginUser(context, userModel);
LoginRepository _loginRepository = LoginRepository();
Map<String, dynamic> dashboardItems =
await _loginRepository.getDashboardItems(userModel.token);
if (dashboardItems['error'] != null) {
print('Fail to fetch the dashboard items from the business details');
print(dashboardItems['error']);
} else {
DashboardItems dashboardItemsModel =
DashboardItems.fromJson(dashboardItems['data']);
signupVm.setDashboardItems(context, dashboardItemsModel);
}
}
}
}
Widget _otpfield(BuildContext context, SignupViewModel signupVm) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 30),
child: PinCodeTextField(
pinBoxWidth: MediaQuery.of(context).size.width / 20,
autofocus: false,
controller: controller,
highlight: true,
highlightColor: Colors.blue,
defaultBorderColor: Colors.black,
hasTextBorderColor: Colors.green,
maxLength: pinLength,
hasError: hasError,
onTextChanged: (text) {
setState(() {
hasError = false;
});
},
onDone: (String value) {
_onDoneVerify(context, signupVm, value);
},
pinCodeTextFieldLayoutType: PinCodeTextFieldLayoutType.NORMAL,
wrapAlignment: WrapAlignment.start,
pinBoxDecoration: ProvidedPinBoxDecoration.underlinedPinBoxDecoration,
pinTextStyle: TextStyle(fontSize: 30.0),
pinTextAnimatedSwitcherTransition:
ProvidedPinBoxTextAnimation.scalingTransition,
pinTextAnimatedSwitcherDuration: Duration(milliseconds: 300),
),
);
}
Widget _verifybutton() {
return Container(
margin: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 30),
child: ButtonTheme(
height: 50,
child: FlatButton(
onPressed: () {
setState(() {
this.thisText = controller.text;
});
},
child: Center(
child: Text(
"VERIFY".toUpperCase(),
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold),
),
),
),
),
decoration: BoxDecoration(
color: colorStyles["primary"],
borderRadius: BorderRadius.circular(5),
),
);
}
void _showOtpPopUp(BuildContext context, SignupViewModel signupVm) async {
print('************Show OTP POPUP**************');
showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Enter OTP'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height / 2.4,
width: MediaQuery.of(context).size.width,
alignment: Alignment.center,
child: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Text(
'We have Texted and/or Emailed OTP (One Time Pin) to your registered cell phone and/ or email account. Please check and enter OTP below to activate your TUDO account.',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 15,
),
textAlign: TextAlign.center,
),
),
_timer(),
_otpfield(context, signupVm),
_otpfieldvalidation(),
SizedBox(
height: 1,
),
_resendbutton("Didn't receive the code? Resend", 30),
SizedBox(
height: 3,
),
_verifybutton(),
],
),
),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Close'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
Widget _resendbutton(String text, int buttontime) {
return ArgonTimerButton(
height: 30,
width: 5,
color: Colors.white,
elevation: 0,
child: Text(
text,
style: TextStyle(
color: colorStyles["primary"],
fontSize: 18,
fontWeight: FontWeight.w700,
),
),
loader: (timeLeft) {
return Text(
"Wait | $timeLeft",
style: TextStyle(
color: Colors.black,
fontSize: 18,
fontWeight: FontWeight.w700,
),
);
},
onTap: (
startTimer,
btnState,
) {
if (btnState == ButtonState.Idle) {
startTimer(buttontime);
print("Resend Code Sended");
_resendCode();
} else {
dispose();
}
},
);
}