From 1050fc4fa02c5c0b4c955ce49aa2ce9b89378486 Mon Sep 17 00:00:00 2001 From: usmanokara Date: Sat, 9 Apr 2022 00:53:58 +0500 Subject: [PATCH 1/5] code --- example/lib/countdown_timer_page.dart | 7 +-- example/pubspec.lock | 65 +++++++++++++++------------ lib/countdown_timer_controller.dart | 19 ++++++-- lib/flutter_countdown_timer.dart | 31 ++++++++----- pubspec.lock | 23 ++++++---- 5 files changed, 89 insertions(+), 56 deletions(-) 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..b003668 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)); @@ -20,6 +24,9 @@ class CountdownTimerController extends ChangeNotifier { ///The end time of the countdown. int _endTime; + ///The end time of the countdown. + int _startTime; + ///Is the countdown running. bool _isRunning = false; @@ -74,8 +81,12 @@ class CountdownTimerController extends ChangeNotifier { ///Calculate current remaining time. CurrentRemainingTime? _calculateCurrentRemainingTime() { - int remainingTimeStamp = - (_endTime - DateTime.now().millisecondsSinceEpoch) ~/ 1000; + int remainingTimeStamp = 0; + if (_endTime != null) + (_endTime - DateTime.now().millisecondsSinceEpoch) ~/ 1000; + else + (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..0e89894 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), 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(); } 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" From 52fbd0a51dd199076bf01ad6ce9288cf5a203a27 Mon Sep 17 00:00:00 2001 From: usmanokara Date: Sat, 9 Apr 2022 01:00:16 +0500 Subject: [PATCH 2/5] code --- lib/flutter_countdown_timer.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/flutter_countdown_timer.dart b/lib/flutter_countdown_timer.dart index 0e89894..2fe9fe9 100644 --- a/lib/flutter_countdown_timer.dart +++ b/lib/flutter_countdown_timer.dart @@ -39,7 +39,7 @@ class CountdownTimer extends StatefulWidget { this.endTime, this.onEnd, this.startTime}) - : assert(endTime != null || controller != null), + : assert(endTime != null || controller != null || startTime != null), super(key: key); @override @@ -86,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(); } From c13210028c12e3abe9ab717b34da4594234d89e5 Mon Sep 17 00:00:00 2001 From: usmanokara Date: Sat, 9 Apr 2022 01:04:56 +0500 Subject: [PATCH 3/5] code --- lib/countdown_timer_controller.dart | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/countdown_timer_controller.dart b/lib/countdown_timer_controller.dart index b003668..9aa35d6 100644 --- a/lib/countdown_timer_controller.dart +++ b/lib/countdown_timer_controller.dart @@ -6,8 +6,8 @@ import 'package:flutter_countdown_timer/index.dart'; ///Countdown timer controller. class CountdownTimerController extends ChangeNotifier { CountdownTimerController( - {required int endTime, - required int startTime, + {required int? endTime, + required int? startTime, this.onEnd, TickerProvider? vsync}) : this._endTime = endTime, @@ -22,10 +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; + int? _startTime; ///Is the countdown running. bool _isRunning = false; @@ -83,9 +83,9 @@ class CountdownTimerController extends ChangeNotifier { CurrentRemainingTime? _calculateCurrentRemainingTime() { int remainingTimeStamp = 0; if (_endTime != null) - (_endTime - DateTime.now().millisecondsSinceEpoch) ~/ 1000; + (_endTime! - DateTime.now().millisecondsSinceEpoch) ~/ 1000; else - (DateTime.now().millisecondsSinceEpoch - _startTime) ~/ 1000; + (DateTime.now().millisecondsSinceEpoch - _startTime!) ~/ 1000; if (remainingTimeStamp <= 0) { return null; From 8e37a98f0fe5d1a0118e3a92b8d59c81d20bf81f Mon Sep 17 00:00:00 2001 From: usmanokara Date: Sat, 9 Apr 2022 01:07:31 +0500 Subject: [PATCH 4/5] code --- lib/flutter_countdown_timer.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/flutter_countdown_timer.dart b/lib/flutter_countdown_timer.dart index 2fe9fe9..0617431 100644 --- a/lib/flutter_countdown_timer.dart +++ b/lib/flutter_countdown_timer.dart @@ -69,9 +69,9 @@ class _CountDownState extends State { initController() { controller = widget.controller ?? CountdownTimerController( - endTime: widget.endTime!, + endTime: widget.endTime, onEnd: widget.onEnd, - startTime: widget.startTime!); + startTime: widget.startTime); if (controller.isRunning == false) { controller.start(); } From 8780762961a6afa9efbcd433757f3d6348f3ece3 Mon Sep 17 00:00:00 2001 From: usmanokara Date: Sat, 9 Apr 2022 01:14:03 +0500 Subject: [PATCH 5/5] code --- lib/countdown_timer_controller.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/countdown_timer_controller.dart b/lib/countdown_timer_controller.dart index 9aa35d6..c0dbbff 100644 --- a/lib/countdown_timer_controller.dart +++ b/lib/countdown_timer_controller.dart @@ -83,9 +83,11 @@ class CountdownTimerController extends ChangeNotifier { CurrentRemainingTime? _calculateCurrentRemainingTime() { int remainingTimeStamp = 0; if (_endTime != null) - (_endTime! - DateTime.now().millisecondsSinceEpoch) ~/ 1000; + remainingTimeStamp = + (_endTime! - DateTime.now().millisecondsSinceEpoch) ~/ 1000; else - (DateTime.now().millisecondsSinceEpoch - _startTime!) ~/ 1000; + remainingTimeStamp = + (DateTime.now().millisecondsSinceEpoch - _startTime!) ~/ 1000; if (remainingTimeStamp <= 0) { return null;