From 53147efa6e1d567804e7a6e635ee527d7036477b Mon Sep 17 00:00:00 2001 From: Vijaykarthik T K <39937989+vijaykarthiktk@users.noreply.github.com> Date: Sat, 4 May 2024 18:21:53 +0530 Subject: [PATCH] Implemented optional pause/resume button in timer --- example/example.dart | 1 + lib/circular_countdown_timer.dart | 79 ++++++++++++++++++++++++++----- pubspec.lock | 70 ++++++++++++++++++--------- 3 files changed, 115 insertions(+), 35 deletions(-) diff --git a/example/example.dart b/example/example.dart index c77a3c6..b5fd137 100644 --- a/example/example.dart +++ b/example/example.dart @@ -121,6 +121,7 @@ class _MyHomePageState extends State { // Here, do whatever you want debugPrint('Countdown Changed $timeStamp'); }, + isControlButtonShown: true, /* * Function to format the text. diff --git a/lib/circular_countdown_timer.dart b/lib/circular_countdown_timer.dart index 4f5eeea..0b8dd27 100644 --- a/lib/circular_countdown_timer.dart +++ b/lib/circular_countdown_timer.dart @@ -78,11 +78,14 @@ class CircularCountDownTimer extends StatefulWidget { /// Handles the timer start. final bool autoStart; - /* + /// Handles visibility of the Pause and Start Button Text + final bool isControlButtonShown; + + /* * Function to format the text. * Allows you to format the current duration to any String. * It also provides the default function in case you want to format specific moments - as in reverse when reaching '0' show 'GO', and for the rest of the instances follow + as in reverse when reaching '0' show 'GO', and for the rest of the instances follow the default behavior. */ final Function(Function(Duration duration) defaultFormatterFunction, @@ -114,6 +117,7 @@ class CircularCountDownTimer extends StatefulWidget { this.autoStart = true, this.textFormat, this.controller, + this.isControlButtonShown = false, }) : assert(initialDuration <= duration); @override @@ -154,6 +158,7 @@ class CircularCountDownTimerState extends State void _setAnimation() { if (widget.autoStart) { + countDownController!.isRunning.value = true; if (widget.isReverse) { _controller!.reverse(from: 1); } else { @@ -274,6 +279,7 @@ class CircularCountDownTimerState extends State _setController(); } + bool pressed = true; @override Widget build(BuildContext context) { return SizedBox( @@ -305,18 +311,58 @@ class CircularCountDownTimerState extends State ), widget.isTimerTextShown ? Align( - alignment: FractionalOffset.center, - child: Text( - time, - style: widget.textStyle ?? - const TextStyle( - fontSize: 16.0, - color: Colors.black, - ), - textAlign: widget.textAlign, + alignment: widget.isControlButtonShown + ? FractionalOffset.topCenter + : FractionalOffset.center, + child: Container( + padding: widget.isControlButtonShown + ? const EdgeInsets.symmetric( + vertical: 50, horizontal: 20) + : null, + child: Text( + time, + style: widget.textStyle ?? + const TextStyle( + fontSize: 16.0, + color: Colors.black, + ), + textAlign: widget.textAlign, + ), ), ) : Container(), + widget.isControlButtonShown + ? Align( + alignment: FractionalOffset.bottomCenter, + child: Padding( + padding: + const EdgeInsets.symmetric(vertical: 10.0), + child: IconButton( + iconSize: 50.0, + padding: const EdgeInsets.symmetric( + vertical: 20, horizontal: 20), + icon: ValueListenableBuilder( + valueListenable: + widget.controller!.isRunning, + builder: (context, isRunning, child) { + return Padding( + padding: const EdgeInsets.all(8.0), + child: isRunning + ? const Icon(Icons.stop) + : const Icon( + Icons.play_arrow_rounded)); + }), + onPressed: () { + if ((widget.controller?.isRunning.value ?? + true) == + true) { + widget.controller?.pause(); + } else { + widget.controller?.resume(); + } + }), + )) + : Container() ], ), ), @@ -340,7 +386,8 @@ class CountDownController { ValueNotifier isStarted = ValueNotifier(false), isPaused = ValueNotifier(false), isResumed = ValueNotifier(false), - isRestarted = ValueNotifier(false); + isRestarted = ValueNotifier(false), + isRunning = ValueNotifier(false); int? _initialDuration, _duration; /// This Method Starts the Countdown Timer @@ -359,6 +406,7 @@ class CountDownController { isPaused.value = false; isResumed.value = false; isRestarted.value = false; + isRunning.value = true; } } @@ -369,6 +417,7 @@ class CountDownController { isPaused.value = true; isRestarted.value = false; isResumed.value = false; + isRunning.value = false; } } @@ -383,6 +432,7 @@ class CountDownController { isResumed.value = true; isRestarted.value = false; isPaused.value = false; + isRunning.value = true; } } @@ -402,6 +452,7 @@ class CountDownController { isRestarted.value = true; isPaused.value = false; isResumed.value = false; + isRunning.value = true; } } @@ -409,10 +460,14 @@ class CountDownController { void reset() { if (_state != null && _state?._controller != null) { _state?._controller?.reset(); + if (_state?.widget.autoStart ?? true) { + start(); + } isStarted.value = _state?.widget.autoStart ?? false; isRestarted.value = false; isPaused.value = false; isResumed.value = false; + isRunning.value = (_state?.widget.autoStart ?? false); } } diff --git a/pubspec.lock b/pubspec.lock index ad74743..2ecf658 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -37,10 +37,10 @@ packages: dependency: transitive description: name: collection - sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c" + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted - version: "1.17.1" + version: "1.18.0" fake_async: dependency: transitive description: @@ -67,14 +67,30 @@ packages: description: flutter source: sdk version: "0.0.0" - js: + leak_tracker: dependency: transitive description: - name: js - sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3 + name: leak_tracker + sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa" url: "https://pub.dev" source: hosted - version: "0.6.7" + version: "10.0.0" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0 + url: "https://pub.dev" + source: hosted + version: "2.0.1" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47 + url: "https://pub.dev" + source: hosted + version: "2.0.1" lints: dependency: transitive description: @@ -87,34 +103,34 @@ packages: dependency: transitive description: name: matcher - sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.15" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724 + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.2.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04 url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.11.0" path: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" sky_engine: dependency: transitive description: flutter @@ -124,26 +140,26 @@ packages: dependency: transitive description: name: source_span - sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250 + sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.10.0" stack_trace: dependency: transitive description: name: stack_trace - sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.11.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" string_scanner: dependency: transitive description: @@ -164,10 +180,10 @@ packages: dependency: transitive description: name: test_api - sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb + sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" url: "https://pub.dev" source: hosted - version: "0.5.1" + version: "0.6.1" vector_math: dependency: transitive description: @@ -176,6 +192,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" + vm_service: + dependency: transitive + description: + name: vm_service + sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957 + url: "https://pub.dev" + source: hosted + version: "13.0.0" sdks: - dart: ">=3.0.0-0 <4.0.0" + dart: ">=3.2.0-0 <4.0.0" flutter: ">=1.17.0"