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

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); }); }

    推荐阅读