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,73 +306,86 @@ 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 ;
337
- isResumed = false ;
332
+ if (_state != null && _state? ._controller != null ) {
333
+ _state? ._controller? .stop (canceled: false );
334
+ isPaused = true ;
335
+ isResumed = false ;
336
+ }
338
337
}
339
338
340
339
/// This Method Resumes the Countdown Timer
341
340
void resume () {
342
- if (_isReverse) {
343
- _state._controller? .reverse (from: _state._controller! .value);
344
- } else {
345
- _state._controller? .forward (from: _state._controller! .value);
341
+ if (_isReverse != null && _state != null && _state? ._controller != null ) {
342
+ if (_isReverse! ) {
343
+ _state? ._controller? .reverse (from: _state! ._controller! .value);
344
+ } else {
345
+ _state? ._controller? .forward (from: _state! ._controller! .value);
346
+ }
347
+ isResumed = true ;
348
+ isPaused = false ;
346
349
}
347
- isResumed = true ;
348
- isPaused = false ;
349
350
}
350
351
351
352
/// This Method Restarts the Countdown Timer,
352
353
/// Here optional int parameter **duration** is the updated duration for countdown timer
353
354
354
355
void restart ({int ? duration}) {
355
- _state._controller! .duration =
356
- Duration (seconds: duration ?? _state._controller! .duration! .inSeconds);
357
- if (_isReverse) {
358
- _state._controller? .reverse (from: 1 );
359
- } else {
360
- _state._controller? .forward (from: 0 );
356
+ if (_isReverse != null && _state != null && _state? ._controller != null ) {
357
+ _state? ._controller! .duration = Duration (
358
+ seconds: duration ?? _state! ._controller! .duration! .inSeconds);
359
+ if (_isReverse! ) {
360
+ _state? ._controller? .reverse (from: 1 );
361
+ } else {
362
+ _state? ._controller? .forward (from: 0 );
363
+ }
364
+ isStarted = true ;
365
+ isPaused = false ;
366
+ isResumed = false ;
361
367
}
362
- isStarted = true ;
363
- isPaused = false ;
364
- isResumed = false ;
365
368
}
366
369
367
370
/// This Method resets the Countdown Timer
368
371
void reset () {
369
- _state._controller? .reset ();
370
- isStarted = _state.widget.autoStart;
371
- isPaused = false ;
372
- isResumed = false ;
372
+ if (_state != null && _state? ._controller != null ) {
373
+ _state? ._controller? .reset ();
374
+ isStarted = _state? .widget.autoStart ?? false ;
375
+ isPaused = false ;
376
+ isResumed = false ;
377
+ }
373
378
}
374
379
375
380
/// This Method returns the **Current Time** of Countdown Timer i.e
376
381
/// Time Used in terms of **Forward Countdown** and Time Left in terms of **Reverse Countdown**
377
382
378
- String getTime () {
379
- return _state
380
- ._getTime (_state._controller! .duration! * _state._controller! .value);
383
+ String ? getTime () {
384
+ if (_state != null && _state? ._controller != null ) {
385
+ return _state? ._getTime (
386
+ _state! ._controller! .duration! * _state! ._controller! .value);
387
+ }
388
+ return "" ;
381
389
}
382
390
}
383
391
0 commit comments