timer_utils.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'dart:async';
  2. typedef void OnTimerTickCallback(int millisUntilFinished);
  3. /*
  4. //示例代码:延时执行
  5. timerUtils = TimerUtils(mDuration: 3000)
  6. ..startDelayTimer()
  7. ..setOnTimerTickCallback((millisUntilFinished) {
  8. print("执行定时任务了:$millisUntilFinished");
  9. });
  10. //示例代码:倒计时60秒
  11. timerUtils = TimerUtils(mDuration: 1000,mTotalTime: 60 * 1000)
  12. ..startCountDown()
  13. ..setOnTimerTickCallback((millisUntilFinished) {
  14. print("执行定时任务了:$millisUntilFinished");
  15. });
  16. */
  17. class TimerUtils {
  18. /// mDuration:间隔时间或延时时间 , 默认为1000毫秒
  19. /// mTotalTime:倒计时的总时间
  20. TimerUtils({this.mDuration = Duration.millisecondsPerSecond, this.mTotalTime = 0});
  21. /// Timer.
  22. Timer? _mTimer;
  23. /// Timer是否启动.
  24. bool _isActive = false;
  25. /// Timer间隔 单位毫秒,默认1000毫秒(1秒).
  26. int mDuration;
  27. /// 倒计时总时间
  28. int mTotalTime; //单位毫秒
  29. /// 回调
  30. OnTimerTickCallback? _onTimerTickCallback;
  31. /// 设置Timer间隔.
  32. void setInterval(int interval) {
  33. if (interval <= 0) interval = Duration.millisecondsPerSecond;
  34. mDuration = interval;
  35. }
  36. /// 设置倒计时总时间.
  37. void setTotalTime(int totalTime) {
  38. if (totalTime <= 0) return;
  39. mTotalTime = totalTime;
  40. }
  41. /// 启动延时任务
  42. void startDelayTimer() {
  43. _mTimer = Timer(Duration(milliseconds: mDuration), () {
  44. _doCallback(0);
  45. });
  46. }
  47. /// 启动定时任务的Timer.例如每隔1秒执行一次
  48. void startPeriodicTimer() {
  49. if (_isActive || mDuration <= 0) return;
  50. _isActive = true;
  51. Duration duration = Duration(milliseconds: mDuration);
  52. _doCallback(0);
  53. _mTimer = Timer.periodic(duration, (Timer timer) {
  54. _doCallback(timer.tick);
  55. });
  56. }
  57. /// 启动倒计时Timer. 每隔1秒执行一次,总共执行长时间
  58. void startCountDown() {
  59. if (_isActive || mDuration <= 0 || mTotalTime <= 0) return;
  60. _isActive = true;
  61. Duration duration = Duration(milliseconds: mDuration);
  62. _doCallback(mTotalTime);
  63. _mTimer = Timer.periodic(duration, (Timer timer) {
  64. int time = mTotalTime - mDuration;
  65. mTotalTime = time;
  66. if (time >= mDuration) {
  67. _doCallback(time);
  68. } else if (time == 0) {
  69. _doCallback(time);
  70. cancel();
  71. } else {
  72. timer.cancel();
  73. Future.delayed(Duration(milliseconds: time), () {
  74. mTotalTime = 0;
  75. _doCallback(0);
  76. cancel();
  77. });
  78. }
  79. });
  80. }
  81. void _doCallback(int time) {
  82. if (_onTimerTickCallback != null) {
  83. _onTimerTickCallback!(time);
  84. }
  85. }
  86. /// 重设倒计时总时间.
  87. void updateTotalTime(int totalTime) {
  88. cancel();
  89. mTotalTime = totalTime;
  90. startCountDown();
  91. }
  92. /// Timer是否启动.
  93. bool isActive() {
  94. return _isActive;
  95. }
  96. /// 取消计时器.
  97. void cancel() {
  98. if (_mTimer != null) {
  99. if (_mTimer!.isActive) {
  100. _mTimer?.cancel();
  101. }
  102. _mTimer = null;
  103. }
  104. _isActive = false;
  105. }
  106. /// 设置Timer的监听
  107. void setOnTimerTickCallback(OnTimerTickCallback callback) {
  108. _onTimerTickCallback = callback;
  109. }
  110. }