Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/countdown_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ class CountdownController extends ValueNotifier<int> {
this.stepDuration = const Duration(milliseconds: 1000),
this.onEnd,
}) : assert((timestamp != null && timestamp > 0) || duration != null),
this._originalDuration = duration,
super((timestamp ?? duration?.inMilliseconds)!);
Timer? _diffTimer;
int? _lastTimestamp;
int? _lostTime;
Duration? _originalDuration;

final Duration stepDuration;

///Event called after the countdown ends
Expand All @@ -32,13 +35,13 @@ class CountdownController extends ValueNotifier<int> {
if (_timestamp >= 3600) {
hours = (_timestamp / 3600).floor();
_timestamp -= hours * 3600;
} else if(days != null) {
} else if (days != null) {
hours = 0;
}
if (_timestamp >= 60) {
min = (_timestamp / 60).floor();
_timestamp -= min * 60;
} else if(hours != null) {
} else if (hours != null) {
min = 0;
}
sec = _timestamp.toInt();
Expand Down Expand Up @@ -86,6 +89,11 @@ class CountdownController extends ValueNotifier<int> {
_dispose();
}

reset() {
value = _originalDuration?.inMilliseconds ?? 0;
_dispose();
}

Duration _getDuration() {
if (_lostTime != null && _lostTime! > 0 && _lostTime! < 1000) {
return Duration(milliseconds: 1000 - _lostTime!);
Expand Down