pausable_timer
3.1.0+3A timer implementation that can be paused, resumed and reset.
Readme
pausable_timer
CI Pub Score Latest Dart version Coverage pub package pub points popularity likes Sponsor (llucax)GitHub SponsorsLiberapayPaypalBuy Me A CoffeePatreonFlattr Sponsor (mateusfccp)GitHub Sponsors
A Dart timer that can be paused, resumed and reset.
Example using start(), pause() and reset()
import 'package:pausable_timer/pausable_timer.dart';
void main() async {
print('Create a timer that fires in 1 second, but it is not started yet');
final timer = PausableTimer(Duration(seconds: 1), () => print('Fired!'));
print('So we start it');
timer.start();
print('And wait 1/2 second...');
await Future<void>.delayed(timer.duration ~/ 2);
print('Not yet fired, still 1/2 second to go!');
print('We can pause it now');
timer.pause();
// When paused, time can pass but the timer won't be fired
print('And we wait a whole second...');
await Future<void>.delayed(timer.duration);
print("But our timer doesn't care while it's paused");
print('It will still wait for ${timer.duration - timer.elapsed} after '
"it's started again");
print('So we start it again');
timer.start();
print('And wait for 1/2 second again, it should have fired when we are done');
await Future<void>.delayed(timer.duration ~/ 2);
print('And we are done, "Fired!" should be up there 👆');
print('Now our timer completed ${timer.tick} tick');
print('We can reset it if we want to use it again');
timer.reset();
print('We have to start it again after the reset because it was not running');
timer.start();
print('Now we wait a whole second in one go...');
await Future<void>.delayed(timer.duration);
print('And we are done, so you should see "Fired!" up there again 👆');
print('Now the timer has ${timer.tick} ticks');
print('We can reset it and start it again');
timer.reset();
timer.start();
print('And you can cancel it too, so it will not fire again');
timer.cancel();
print("After a timer is cancelled, it can't be used again");
print('But important information can still be retrieved:');
print('duration: ${timer.duration}');
print('elapsed: ${timer.elapsed}');
print('tick: ${timer.tick}');
print('isPaused: ${timer.isPaused}');
print('isActive: ${timer.isActive}');
print('isExpired: ${timer.isExpired}');
print('isCancelled: ${timer.isCancelled}');
}
Example pausable countdown implementation
// Example on how to implement countdown using PausableTimer.periodic
import 'dart:async';
import 'package:pausable_timer/pausable_timer.dart';
void main() async {
// We make it "late" to be able to use the timer in the timer's callback.
late final PausableTimer timer;
var countDown = 5;
print('Create a periodic timer that fires every 1 second and starts it');
timer = PausableTimer.periodic(
Duration(seconds: 1),
() {
countDown--;
if (countDown == 0) {
timer.pause();
}
print('\t$countDown');
},
)..start();
print('And wait 2.1 seconds...');
print('(0.1 extra to make sure there is no race between the timer and the '
'waiting here)');
await Future<void>.delayed(timer.duration * 2.1);
print('By now 2 events should have fired: 4, 3\n');
print('We can pause it now');
timer.pause();
print('And we wait for 2 more seconds...');
await Future<void>.delayed(timer.duration * 2);
print("But our timer doesn't care while it's paused\n");
print('So we start it again');
timer.start();
print('And wait for 3.1 seconds more...');
await Future<void>.delayed(timer.duration * 3.1);
print('And we are done: 2, 1 and 0 should have been printed');
print('The timer should be unpaused, inactive, expired and not cancelled');
print('isPaused: ${timer.isPaused}');
print('isActive: ${timer.isActive}');
print('isExpired: ${timer.isExpired}');
print('isCancelled: ${timer.isCancelled}');
print('We can now reset it and start it again, now for 3 seconds');
countDown = 3;
timer
..reset()
..start();
print('And wait for 3.1 seconds...');
await Future<void>.delayed(timer.duration * 3.1);
print('And it should be done printing: 2, 1 and 0');
}
Changelog
3.1.0+3
- Fix README.md reference to
yamlfile
3.1.0+2
- Update README.md countdown example to use
PausableTimer.periodic - Rename
.ymlfiles to.yaml, which is the recommended extension - Use single quote in
wip.yamlso that backticks won't expand to commands
3.1.0+1
- Automate pub.dev publishing with GitHub Actions
3.1.0
- Provide
.periodicconstructor (#45)- A periodic timer should behave similar to Timer.periodic
3.0.0
- BREAKING:
PausableTimeris nowfinal - Minor cleanups and fixes in documentations and tests
2.0.0+1
- Update and test against Dart 3.1.
More details available in GitHub.
2.0.0
- Update and test against Dart 3.0.
- Bump dependencies and configuration to work with Dart 3.0.
- Because of the above, drop support for Dart 2.x.
NOTE: If you need to use Dart 2.x you can stick to the 1.x branch series. Even more, those releases seem to be forward-compatible with Dart 3.0, so upgrading should only be necessary if you also need to bump other dependencies.
WANTED: It's been almost 2 years since the last time I used Dart or Flutter, and while I enjoy maintaining open source projects, it is becoming more and more difficult to keep up to date with changes in Dart and maintaining this project, so I'm looking for a maintainer willing to take over this project to ensure its health in the future. Please get in touch if you are interested.
More details available in GitHub.
1.0.0+7
- Update and test against Dart 2.19.
More details available in GitHub.
1.0.0+6
- Add funding link to
pubspec.yaml. - Update and test against Dart 2.18.
More details available in GitHub.
1.0.0+5
- Update and test against Dart 2.17.
More details available in GitHub.
1.0.0+4
- Update and test against Dart 2.16.
More details available in GitHub.
1.0.0+3
- Update and test against Dart 2.15.
- Update dependencies.
More details available in GitHub.
1.0.0+2
- Improve CI of the project
- Update example to use
lateinstead of an extra variable to avoid!. - Update and test against Dart 2.14.
More details available in GitHub.
1.0.0+1
This is a symbolic release marking that this package is stable (it has been used for some time now without further issues) and there is no plan to change the API in a near future.
The code is exacly the same as in version v0.2.0+1.
1.0.0
Unpublished version due to errors in the release process.
0.2.0+1
- Fix minimum dart SDK version (use a final release, not a pre-release).
NOTE: Unless critical issues are found, this release will be released as 1.0.0 with no other changes since it has been pretty stable for a while now, and no API breaking changes are expected for a long while either.
0.2.0
- Bump dependencies.
NOTE: Due to a wrongly specified minimum dart SDK version, this version was never published in pub.dev.
0.2.0-nullsafety.0
-
Make package null-safe.
NOTE: This means the minimum supported Dart version has been bumped to 2.12.
0.1.0+2
- Add example implementing a pausable countdown.
0.1.0+1
- Add build status and sponsorship badges.
0.1.0
- Initial version.
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add pausable_timerpubspec.yaml
name: pausable_timer
description: A timer implementation that can be paused, resumed and reset.
homepage: https://github.com/llucax/pausable_timer
repository: https://github.com/llucax/pausable_timer
funding:
- https://github.com/sponsors/mateusfccp
- https://github.com/llucax/llucax/blob/main/sponsoring-platforms.md
version: 3.1.0+3
environment:
sdk: ">=3.0.0 <4.0.0"
dependencies:
clock: ^1.1.0
dev_dependencies:
coverage: ^1.7.1
fake_async: ^1.3.1
lints: ^3.0.0
test: ^1.24.8
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.1.0 | On demand | Download | 510c8a4a…1b59ba |
| 0.1.0+1 | On demand | Download | 0864ffc9…55e713 |
| 0.1.0+2 | On demand | Download | 2d210d63…f53b4a |
| 0.2.0-nullsafety.0 | On demand | Download | e76bbb45…cafc1d |
| 0.2.0+1 | On demand | Download | c7a2558d…4bb608 |
| 1.0.0+1 | On demand | Download | ef1b603a…fee43d |
| 1.0.0+2 | On demand | Download | f3356a67…41244c |
| 1.0.0+3 | On demand | Download | 08e1f7ef…304b9c |
| 1.0.0+4 | On demand | Download | 5d0ba905…2d7f80 |
| 1.0.0+5 | On demand | Download | 65d57265…00297f |
| 1.0.0+6 | On demand | Download | 6a63767e…2ddea8 |
| 1.0.0+7 | On demand | Download | 1897e2c6…efc0a6 |
| 2.0.0 | On demand | Download | d0fb70de…c5246d |
| 2.0.0+1 | On demand | Download | 35b1f77e…84659a |
| 3.0.0 | On demand | Download | ff810701…86ef82 |
| 3.1.0 | On demand | Download | 6d65343d…e50809 |
| 3.1.0+1 | On demand | Download | 18565ca3…592984 |
| 3.1.0+2 | On demand | Download | 874f3e03…5a3f2e |
| 3.1.0+3 | Cached · Upstream SHA-256 matched | Download | 6ef1a954…e86074 |
Dependencies
clock "^1.1.0"