startCountdown method

void startCountdown(
  1. int restOfTime,
  2. CountdownCallback? callback, {
  3. int step = 1000,
})

开始倒计时 restOfTime 剩余时间(单位毫秒) callback 回调(null时不做计时处理) step 计时步长默认1秒回调一次

Implementation

void startCountdown(int restOfTime, CountdownCallback? callback, {int step = 1000}) {
  if (restOfTime <= 0 || callback == null) {
    return;
  }
  stop();
  _isFinish = false;
  _timer = Timer.periodic(Duration(milliseconds: step), (timer) {
    if (restOfTime > 0) {
      restOfTime -= step;
      _diffTime = restOfTime;
      int diff = restOfTime;
      int days = diff ~/ _dayUnit;
      diff = diff - days * _dayUnit;
      int hours = diff ~/ _hourUnit;
      diff = diff - hours * _hourUnit;
      int minute = diff ~/ _minuteUnit;
      diff = diff - minute * _minuteUnit;
      int second = diff ~/ _secondUnit;
      if (days == 0 && hours == 0 && minute == 0 && second == 0) {
        stop();
      }
      callback(days, hours, minute, second);
    } else {
      stop();
      if (!_isFinish) {
        callback(0, 0, 0, 0);
      }
    }
  });
}