diff --git a/example/lib/countdown_timer_page.dart b/example/lib/countdown_timer_page.dart index 716d022..d6a9dde 100644 --- a/example/lib/countdown_timer_page.dart +++ b/example/lib/countdown_timer_page.dart @@ -14,8 +14,8 @@ class _CountdownTimerPageState extends State { @override void initState() { super.initState(); - controller = - CountdownTimerController(endTime: endTime, onEnd: onEnd); + controller = CountdownTimerController( + endTime: endTime, onEnd: onEnd, startTime: null); } void onEnd() { @@ -92,7 +92,8 @@ class _CountdownTimerPageState extends State { AnimatedBuilder( animation: time.milliseconds!, builder: (context, child) { - return Text("${(time.milliseconds!.value * 1000).toInt()}"); + return Text( + "${(time.milliseconds!.value * 1000).toInt()}"); }, ) ], diff --git a/example/pubspec.lock b/example/pubspec.lock index 07c5146..2fd9b29 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -5,56 +5,56 @@ packages: dependency: transitive description: name: async - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "2.5.0" + version: "2.8.2" boolean_selector: dependency: transitive description: name: boolean_selector - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "2.1.0" characters: dependency: transitive description: name: characters - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" charcode: dependency: transitive description: name: charcode - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.1" clock: dependency: transitive description: name: clock - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "1.1.0" collection: dependency: transitive description: name: collection - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "1.15.0" cupertino_icons: dependency: "direct main" description: name: cupertino_icons - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "0.1.3" fake_async: dependency: transitive description: name: fake_async - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "1.2.0" flutter: @@ -68,7 +68,7 @@ packages: path: ".." relative: true source: path - version: "3.0.1" + version: "4.1.0" flutter_test: dependency: "direct dev" description: flutter @@ -78,21 +78,28 @@ packages: dependency: transitive description: name: matcher - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.11" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" source: hosted - version: "0.12.10" + version: "0.1.3" meta: dependency: transitive description: name: meta - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.3.0" + version: "1.7.0" path: dependency: transitive description: name: path - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "1.8.0" sky_engine: @@ -104,57 +111,57 @@ packages: dependency: transitive description: name: source_span - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" stack_trace: dependency: transitive description: name: stack_trace - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "1.2.0" test_api: dependency: transitive description: name: test_api - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "0.2.19" + version: "0.4.8" typed_data: dependency: transitive description: name: typed_data - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted version: "1.3.0" vector_math: dependency: transitive description: name: vector_math - url: "https://pub.flutter-io.cn" + url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.1" sdks: - dart: ">=2.12.0 <3.0.0" + dart: ">=2.14.0 <3.0.0" diff --git a/lib/countdown_timer_controller.dart b/lib/countdown_timer_controller.dart index e666749..c0dbbff 100644 --- a/lib/countdown_timer_controller.dart +++ b/lib/countdown_timer_controller.dart @@ -6,8 +6,12 @@ import 'package:flutter_countdown_timer/index.dart'; ///Countdown timer controller. class CountdownTimerController extends ChangeNotifier { CountdownTimerController( - {required int endTime, this.onEnd, TickerProvider? vsync}) - : this._endTime = endTime { + {required int? endTime, + required int? startTime, + this.onEnd, + TickerProvider? vsync}) + : this._endTime = endTime, + this._startTime = startTime { if (vsync != null) { this._animationController = AnimationController(vsync: vsync, duration: Duration(seconds: 1)); @@ -18,7 +22,10 @@ class CountdownTimerController extends ChangeNotifier { final VoidCallback? onEnd; ///The end time of the countdown. - int _endTime; + int? _endTime; + + ///The end time of the countdown. + int? _startTime; ///Is the countdown running. bool _isRunning = false; @@ -74,8 +81,14 @@ class CountdownTimerController extends ChangeNotifier { ///Calculate current remaining time. CurrentRemainingTime? _calculateCurrentRemainingTime() { - int remainingTimeStamp = - (_endTime - DateTime.now().millisecondsSinceEpoch) ~/ 1000; + int remainingTimeStamp = 0; + if (_endTime != null) + remainingTimeStamp = + (_endTime! - DateTime.now().millisecondsSinceEpoch) ~/ 1000; + else + remainingTimeStamp = + (DateTime.now().millisecondsSinceEpoch - _startTime!) ~/ 1000; + if (remainingTimeStamp <= 0) { return null; } diff --git a/lib/flutter_countdown_timer.dart b/lib/flutter_countdown_timer.dart index b285642..0617431 100644 --- a/lib/flutter_countdown_timer.dart +++ b/lib/flutter_countdown_timer.dart @@ -25,17 +25,21 @@ class CountdownTimer extends StatefulWidget { ///The end time of the countdown. final int? endTime; - CountdownTimer({ - Key? key, - this.endWidget = const Center( - child: Text('The current time has expired'), - ), - this.widgetBuilder, - this.controller, - this.textStyle, - this.endTime, - this.onEnd, - }) : assert(endTime != null || controller != null), + ///The end time of the countdown. + final int? startTime; + + CountdownTimer( + {Key? key, + this.endWidget = const Center( + child: Text('The current time has expired'), + ), + this.widgetBuilder, + this.controller, + this.textStyle, + this.endTime, + this.onEnd, + this.startTime}) + : assert(endTime != null || controller != null || startTime != null), super(key: key); @override @@ -64,7 +68,10 @@ class _CountDownState extends State { ///Generate countdown controller. initController() { controller = widget.controller ?? - CountdownTimerController(endTime: widget.endTime!, onEnd: widget.onEnd); + CountdownTimerController( + endTime: widget.endTime, + onEnd: widget.onEnd, + startTime: widget.startTime); if (controller.isRunning == false) { controller.start(); } @@ -79,7 +86,8 @@ class _CountDownState extends State { void didUpdateWidget(CountdownTimer oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.endTime != widget.endTime || - widget.controller != oldWidget.controller) { + widget.controller != oldWidget.controller || + widget.startTime == oldWidget.startTime) { controller.dispose(); initController(); } diff --git a/pubspec.lock b/pubspec.lock index ad6d1df..3317f5f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.6.1" + version: "2.8.2" boolean_selector: dependency: transitive description: @@ -21,14 +21,14 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.1" clock: dependency: transitive description: @@ -66,14 +66,21 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10" + version: "0.12.11" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.3" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0" + version: "1.7.0" path: dependency: transitive description: @@ -127,7 +134,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.3.0" + version: "0.4.8" typed_data: dependency: transitive description: @@ -141,6 +148,6 @@ packages: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.1" sdks: - dart: ">=2.12.0 <3.0.0" + dart: ">=2.14.0 <3.0.0"