美文网首页
flutter ,dart 和当前时间对比倒计时

flutter ,dart 和当前时间对比倒计时

作者: 曾经也是个少年 | 来源:发表于2019-11-08 10:51 被阅读0次
    void _timeFunc(time){
        var _newDate = DateTime.now();
        const period = const Duration(seconds: 1);
        print(time);
        var _diffDate = DateTime.parse(time.toString());
        _timerIndex = Timer.periodic(period, (timer) {
          //到时回调
          _diffDate =  _diffDate.subtract(Duration(seconds: 1));
          // count++;
          if (_diffDate.difference(_newDate).inSeconds <= 0) {
            //取消定时器,避免无限回调
            timer.cancel();
            timer = null;
          }
          // print();
          var _surplus = _diffDate.difference(_newDate);
          int _day = (_surplus.inSeconds ~/ 3600) ~/ 24;
          int hour = (_surplus.inSeconds ~/ 3600) % 24;
          int minute = _surplus.inSeconds % 3600 ~/ 60;
          int second = _surplus.inSeconds % 60;
        // formatTime(hour) + ":" + formatTime(minute) + ":" + formatTime(second);
          setState(() {
            _day1 = _day.toString()??'0';
            _hour1 = hour.toString()??'0';
            _minute1 = minute.toString()??'0';
            _second1 = second.toString()??'0';
            // _text = _diffDate.weekday.toString()+'  ' +_diffDate.hour.toString()+':' + _diffDate.minute.toString() +':'+ _diffDate.second.toString();
          });
          // debugPrint(_text);
        });
      }
    

    相关文章

      网友评论

          本文标题:flutter ,dart 和当前时间对比倒计时

          本文链接:https://www.haomeiwen.com/subject/wjsibctx.html