一个可以配置为一次性或重复触发的计时器,具有启动、停止、继续和取消的能力。

入门

CompleteTimer 添加到您的 pubspec.yaml 文件中

dependencies:
  complete_timer: ^1.0.0

在将要使用 CompleteTimer 的文件中导入它

import 'package:complete_timer/complete_timer.dart';

如何使用?

创建一个 CompleteTimer,并提供持续时间和您希望在给定持续时间后触发的回调。

// By default the timer starts automatically.
final CompleteTimer timer = CompleteTimer(
    duration: Duration(seconds: 5),
    // The callback function is invoked after the given duration.
    callback: (timer) {
      print('timer finished');
    },
  );

参数

  final CompleteTimer normalTimer = CompleteTimer(
    // must a non-negative Duration.
    duration: Duration(seconds: 5),
    
    // If periodic sets true
    // The callback is invoked repeatedly with duration intervals until
    // canceled with the cancel function.
    // Defaults to false.
    periodic: false,
    
    // If autoStart sets true timer starts automatically, default to true.
    autoStart: true,
    
    // The callback function is invoked after the given duration.
    callback: (timer) {
      print('normal example');
      timer.stop();
      // We call stop function before getting elapsed time to get a exact time.
      print('normal timer finished after: ${timer.elapsed}');
    },
  );

CompleteTimer 方法

  • start()

      Start the timer, you don't need to call it if autoStart sets true.
      If call when the timer already started, then it resume the timer
      which this means elapsed and tick starts increasing from their previous value.
    
  • stop()

      Stop the timer.
      The elapsed and tick stops increasing after this call.
      If call when timer is stopped does nothing.
    
  • cancel()

      Cancel the timer.
      The elapsed and tick resets after this call.
    
  • elapsed

      Elapsed time.
    
  • tick

      The value starts at zero and is incremented each time a timer event
      occurs, so each callback will see a larger value than the previous one.
    
  • isRunning

     Whether the [CompleteTimer] is currently running.
    

GitHub

查看 Github