Skip to content

Commit 0c40529

Browse files
Added timeFormatterFunction.
1 parent 49f8084 commit 0c40529

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

example/example.dart

+17
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ class _MyHomePageState extends State<MyHomePage> {
118118
// Here, do whatever you want
119119
debugPrint('Countdown Changed $timeStamp');
120120
},
121+
122+
/*
123+
* Function to format the text.
124+
* Allows you to format the current duration to any String.
125+
* It also provides the default function in case you want to format specific moments
126+
as in reverse when reaching '0' show 'GO', and for the rest of the instances follow
127+
the default behavior.
128+
*/
129+
timeFormatterFunction: (defaultFormatterFunction, duration) {
130+
if (duration.inSeconds == 0) {
131+
// only format for '0'
132+
return "Start";
133+
} else {
134+
// other durations by it's default format
135+
return Function.apply(defaultFormatterFunction, [duration]);
136+
}
137+
},
121138
),
122139
),
123140
floatingActionButton: Row(

lib/circular_countdown_timer.dart

+34-3
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,23 @@ class CircularCountDownTimer extends StatefulWidget {
7373
/// Handles the timer start.
7474
final bool autoStart;
7575

76+
/*
77+
* Function to format the text.
78+
* Allows you to format the current duration to any String.
79+
* It also provides the default function in case you want to format specific moments
80+
as in reverse when reaching '0' show 'GO', and for the rest of the instances follow
81+
the default behavior.
82+
*/
83+
final Function(Function(Duration duration) defaultFormatterFunction,
84+
Duration duration)? timeFormatterFunction;
85+
7686
const CircularCountDownTimer({
7787
required this.width,
7888
required this.height,
7989
required this.duration,
8090
required this.fillColor,
8191
required this.ringColor,
92+
this.timeFormatterFunction,
8293
this.backgroundColor,
8394
this.fillGradient,
8495
this.ringGradient,
@@ -114,10 +125,20 @@ class CircularCountDownTimerState extends State<CircularCountDownTimer>
114125
if (widget.isReverse &&
115126
!widget.autoStart &&
116127
!countDownController!.isStarted) {
117-
timeStamp = _getTime(Duration(seconds: widget.duration));
128+
if (widget.timeFormatterFunction != null) {
129+
return Function.apply(widget.timeFormatterFunction!,
130+
[_getTime, Duration(seconds: widget.duration)]).toString();
131+
} else {
132+
timeStamp = _getTime(Duration(seconds: widget.duration));
133+
}
118134
} else {
119135
Duration? duration = _controller!.duration! * _controller!.value;
120-
timeStamp = _getTime(duration);
136+
if (widget.timeFormatterFunction != null) {
137+
return Function.apply(
138+
widget.timeFormatterFunction!, [_getTime, duration]).toString();
139+
} else {
140+
timeStamp = _getTime(duration);
141+
}
121142
}
122143
if (widget.onChange != null) widget.onChange!(timeStamp);
123144

@@ -308,7 +329,10 @@ class CircularCountDownTimerState extends State<CircularCountDownTimer>
308329
class CountDownController {
309330
CircularCountDownTimerState? _state;
310331
bool? _isReverse;
311-
bool isStarted = false, isPaused = false, isResumed = false;
332+
bool isStarted = false,
333+
isPaused = false,
334+
isResumed = false,
335+
isRestarted = false;
312336
int? _initialDuration, _duration;
313337

314338
/// This Method Starts the Countdown Timer
@@ -324,6 +348,9 @@ class CountDownController {
324348
from: _initialDuration == 0 ? 0 : (_initialDuration! / _duration!));
325349
}
326350
isStarted = true;
351+
isPaused = false;
352+
isResumed = false;
353+
isRestarted = false;
327354
}
328355
}
329356

@@ -332,6 +359,7 @@ class CountDownController {
332359
if (_state != null && _state?._controller != null) {
333360
_state?._controller?.stop(canceled: false);
334361
isPaused = true;
362+
isRestarted = false;
335363
isResumed = false;
336364
}
337365
}
@@ -345,6 +373,7 @@ class CountDownController {
345373
_state?._controller?.forward(from: _state!._controller!.value);
346374
}
347375
isResumed = true;
376+
isRestarted = false;
348377
isPaused = false;
349378
}
350379
}
@@ -362,6 +391,7 @@ class CountDownController {
362391
_state?._controller?.forward(from: 0);
363392
}
364393
isStarted = true;
394+
isRestarted = true;
365395
isPaused = false;
366396
isResumed = false;
367397
}
@@ -372,6 +402,7 @@ class CountDownController {
372402
if (_state != null && _state?._controller != null) {
373403
_state?._controller?.reset();
374404
isStarted = _state?.widget.autoStart ?? false;
405+
isRestarted = false;
375406
isPaused = false;
376407
isResumed = false;
377408
}

0 commit comments

Comments
 (0)