startTimer method
Starts the countdown timer.
If a startTime is provided, the timer will start from that duration;
otherwise, it starts from the current remaining time.
Implementation
void startTimer({Duration? startTime}) {
_timer?.cancel();
if (startTime != null) {
_secondsRemaining = startTime.inSeconds;
}
// Notify listeners immediately of the starting state.
notifyListeners();
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
// If no time remains, ensure the timer is cancelled.
if (_secondsRemaining <= 0) {
timer.cancel();
return;
}
_secondsRemaining--;
if (_secondsRemaining <= 0) {
// Ensure we don't go negative.
_secondsRemaining = 0;
timer.cancel();
notifyListeners();
if (onTimerEnd != null) {
onTimerEnd!();
}
} else {
notifyListeners();
}
});
}