1
+ // ignore_for_file: constant_identifier_names
2
+
1
3
library circular_countdown_timer;
2
4
3
5
import 'package:flutter/material.dart' ;
4
6
import 'custom_timer_painter.dart' ;
5
7
6
8
/// Create a Circular Countdown Timer.
7
9
class CircularCountDownTimer extends StatefulWidget {
8
- /// Key for Countdown Timer.
9
- final Key ? key;
10
-
11
10
/// Filling Color for Countdown Widget.
12
11
final Color fillColor;
13
12
@@ -74,7 +73,7 @@ class CircularCountDownTimer extends StatefulWidget {
74
73
/// Handles the timer start.
75
74
final bool autoStart;
76
75
77
- CircularCountDownTimer ({
76
+ const CircularCountDownTimer ({
78
77
required this .width,
79
78
required this .height,
80
79
required this .duration,
@@ -93,18 +92,15 @@ class CircularCountDownTimer extends StatefulWidget {
93
92
this .strokeWidth = 5.0 ,
94
93
this .strokeCap = StrokeCap .butt,
95
94
this .textStyle,
96
- this .key,
95
+ super .key,
97
96
this .isTimerTextShown = true ,
98
97
this .autoStart = true ,
99
98
this .textFormat,
100
99
this .controller,
101
- }) : assert (initialDuration <= duration),
102
- super (key: key);
100
+ }) : assert (initialDuration <= duration);
103
101
104
102
@override
105
- CircularCountDownTimerState createState () => CircularCountDownTimerState (
106
- countDownController: controller ?? CountDownController (),
107
- );
103
+ CircularCountDownTimerState createState () => CircularCountDownTimerState ();
108
104
}
109
105
110
106
class CircularCountDownTimerState extends State <CircularCountDownTimer >
@@ -113,8 +109,6 @@ class CircularCountDownTimerState extends State<CircularCountDownTimer>
113
109
Animation <double >? _countDownAnimation;
114
110
CountDownController ? countDownController;
115
111
116
- CircularCountDownTimerState ({this .countDownController});
117
-
118
112
String get time {
119
113
String timeStamp = "" ;
120
114
if (widget.isReverse &&
@@ -185,7 +179,7 @@ class CircularCountDownTimerState extends State<CircularCountDownTimer>
185
179
}
186
180
// For ss format
187
181
else if (widget.textFormat == CountdownTextFormat .SS ) {
188
- return '${ (duration .inSeconds ).toString ().padLeft (2 , '0' )}' ;
182
+ return (duration.inSeconds).toString ().padLeft (2 , '0' );
189
183
}
190
184
// For s format
191
185
else if (widget.textFormat == CountdownTextFormat .S ) {
@@ -216,6 +210,7 @@ class CircularCountDownTimerState extends State<CircularCountDownTimer>
216
210
217
211
@override
218
212
void initState () {
213
+ countDownController = widget.controller ?? CountDownController ();
219
214
super .initState ();
220
215
_controller = AnimationController (
221
216
vsync: this ,
@@ -253,7 +248,7 @@ class CircularCountDownTimerState extends State<CircularCountDownTimer>
253
248
254
249
@override
255
250
Widget build (BuildContext context) {
256
- return Container (
251
+ return SizedBox (
257
252
width: widget.width,
258
253
height: widget.height,
259
254
child: AnimatedBuilder (
@@ -286,7 +281,7 @@ class CircularCountDownTimerState extends State<CircularCountDownTimer>
286
281
child: Text (
287
282
time,
288
283
style: widget.textStyle ??
289
- TextStyle (
284
+ const TextStyle (
290
285
fontSize: 16.0 ,
291
286
color: Colors .black,
292
287
),
@@ -311,71 +306,84 @@ class CircularCountDownTimerState extends State<CircularCountDownTimer>
311
306
312
307
/// Controls (i.e Start, Pause, Resume, Restart) the Countdown Timer.
313
308
class CountDownController {
314
- late CircularCountDownTimerState _state;
315
- late bool _isReverse;
309
+ CircularCountDownTimerState ? _state;
310
+ bool ? _isReverse;
316
311
bool isStarted = false , isPaused = false , isResumed = false ;
317
312
int ? _initialDuration, _duration;
318
313
319
314
/// This Method Starts the Countdown Timer
320
315
void start () {
321
- if (_isReverse) {
322
- _state._controller? .reverse (
323
- from:
324
- _initialDuration == 0 ? 1 : 1 - (_initialDuration! / _duration! ));
325
- } else {
326
- _state._controller? .forward (
327
- from: _initialDuration == 0 ? 0 : (_initialDuration! / _duration! ));
316
+ if (_isReverse != null && _state != null && _state? ._controller != null ) {
317
+ if (_isReverse! ) {
318
+ _state? ._controller? .reverse (
319
+ from: _initialDuration == 0
320
+ ? 1
321
+ : 1 - (_initialDuration! / _duration! ));
322
+ } else {
323
+ _state? ._controller? .forward (
324
+ from: _initialDuration == 0 ? 0 : (_initialDuration! / _duration! ));
325
+ }
326
+ isStarted = true ;
328
327
}
329
-
330
- isStarted = true ;
331
328
}
332
329
333
330
/// This Method Pauses the Countdown Timer
334
331
void pause () {
335
- _state._controller? .stop (canceled: false );
336
- isPaused = true ;
332
+ if (_state != null && _state? ._controller != null ) {
333
+ _state? ._controller? .stop (canceled: false );
334
+ isPaused = true ;
335
+ }
337
336
}
338
337
339
338
/// This Method Resumes the Countdown Timer
340
339
void resume () {
341
- if (_isReverse) {
342
- _state._controller? .reverse (from: _state._controller! .value);
343
- } else {
344
- _state._controller? .forward (from: _state._controller! .value);
340
+ if (_isReverse != null && _state != null && _state? ._controller != null ) {
341
+ if (_isReverse! ) {
342
+ _state? ._controller? .reverse (from: _state! ._controller! .value);
343
+ } else {
344
+ _state? ._controller? .forward (from: _state! ._controller! .value);
345
+ }
346
+ isResumed = true ;
345
347
}
346
- isResumed = true ;
347
348
}
348
349
349
350
/// This Method Restarts the Countdown Timer,
350
351
/// Here optional int parameter **duration** is the updated duration for countdown timer
351
352
352
353
void restart ({int ? duration}) {
353
- _state._controller! .duration =
354
- Duration (seconds: duration ?? _state._controller! .duration! .inSeconds);
355
- if (_isReverse) {
356
- _state._controller? .reverse (from: 1 );
357
- } else {
358
- _state._controller? .forward (from: 0 );
354
+ if (_isReverse != null && _state != null && _state? ._controller != null ) {
355
+ _state? ._controller! .duration = Duration (
356
+ seconds: duration ?? _state! ._controller! .duration! .inSeconds);
357
+ if (_isReverse! ) {
358
+ _state? ._controller? .reverse (from: 1 );
359
+ } else {
360
+ _state? ._controller? .forward (from: 0 );
361
+ }
362
+ isStarted = true ;
363
+ isPaused = false ;
364
+ isResumed = false ;
359
365
}
360
- isStarted = true ;
361
- isPaused = false ;
362
- isResumed = false ;
363
366
}
364
367
365
368
/// This Method resets the Countdown Timer
366
369
void reset () {
367
- _state._controller? .reset ();
368
- isStarted = _state.widget.autoStart;
369
- isPaused = false ;
370
- isResumed = false ;
370
+ if (_state != null && _state? ._controller != null ) {
371
+ _state? ._controller? .reset ();
372
+ isStarted = _state? .widget.autoStart ?? false ;
373
+ isPaused = false ;
374
+ isResumed = false ;
375
+ }
371
376
}
372
377
373
378
/// This Method returns the **Current Time** of Countdown Timer i.e
374
379
/// Time Used in terms of **Forward Countdown** and Time Left in terms of **Reverse Countdown**
375
380
376
- String getTime () {
377
- return _state
378
- ._getTime (_state._controller! .duration! * _state._controller! .value);
381
+ String ? getTime () {
382
+ if (_state != null && _state? ._controller != null ) {
383
+ return _state? ._getTime (
384
+ _state! ._controller! .duration! * _state! ._controller! .value);
385
+ }
386
+ return "" ;
379
387
}
380
388
}
381
389
0 commit comments