setPresetTime method

void setPresetTime({
  1. required int mSec,
  2. bool add = true,
})

Set preset time. 1000 mSec => 1 sec

Implementation

void setPresetTime({required int mSec, bool add = true}) {
  switch (mode) {
    case StopWatchMode.countUp:
      final currentTotalTime = _getCountUpTime();
      final currentTimeWithoutPreset = currentTotalTime - _presetTime;
      if (add) {
        if (mSec < 0 && currentTotalTime + mSec <= 0) {
          // total time will be 0
          _presetTime = -currentTimeWithoutPreset;
        } else {
          _presetTime += mSec;
        }
      } else {
        if (mSec < 0 && currentTimeWithoutPreset + mSec < 0) {
          _presetTime = -currentTimeWithoutPreset;
        } else {
          _presetTime = mSec;
        }
      }
      break;
    case StopWatchMode.countDown:
      final currentRemainingTime = _getCountDownTime();
      final currentElapsedTime = _presetTime - currentRemainingTime;
      if (add) {
        if (mSec < 0 && currentRemainingTime + mSec <= 0) {
          // total time will be 0
          _presetTime = currentElapsedTime;
        } else {
          _presetTime += mSec;
        }
      } else {
        if (mSec < 0 && currentElapsedTime + mSec < 0) {
          _presetTime = currentElapsedTime;
        } else {
          _presetTime = mSec;
        }
      }
      break;
  }
  _elapsedTime.add(
    mode == StopWatchMode.countUp ? _getCountUpTime() : _getCountDownTime(),
  );
}