Flutter

小白学Flutter - 验证码倒计时

2023-11-27  本文已影响0人  林希品
//定义timer
Timer? myTimer;

//Time初始化和倒计时
myTimer = Timer.periodic(Duration(seconds: 1), (timer) {
    if (state.initialCountdown > 0) {
       state.initialCountdown--;
       print("点击倒计时${state.initialCountdown}");
       state.verificationCode = "重新发送(${state.initialCountdown})";
    } else {
       state.timer.cancel();
       state.verificationCode = "获取验证码";
       state.isGetCodeButtonEnabled = true;
    }
});

// 判断是否已经初始化
  if (myTimer != null) {
    print('Timer is initialized.');
  } else {
    print('Timer is not initialized.');
  }

 // 在合适的地方 取消 Timer
  myTimer?.cancel();


上一篇下一篇

猜你喜欢

热点阅读