引入

import 'dart:async';

使用定时器 Timer.periodic

定时器  实例 :倒计时
  String buttonText = '发送验证码';
  int count = 60; //初始倒计时时间
  Timer timer; //倒计时的计时器
  
  // 倒计时状态控制
  void _initTimer() {
    timer = new Timer.periodic(Duration(seconds: 1), (Timer timer) {
      count--;
      //数据操作处理
      setState(() {
        if (count == 0) {
          timer.cancel(); //倒计时结束取消定时器
          count = 60; //重置时间
          buttonText = '发送验证码'; //重置按钮文本
        } else {
          buttonText = '重新发送($count)'; //更新文本内容
        }
      });
    });
  }

使用延时器``

Timer timer;

 new Future.delayed(Duration(seconds: 1), () {
        Navigator.pop(context);//延时操作
});
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐