TimeBuilder.countdown constructor
TimeBuilder.countdown({
- Key? key,
- required DateTime start,
- required int seconds,
- required CountdownWidgetBuilder builder,
Creates a countdown, from the given start
DateTime.
Will call the builder
, which is a CountdownWidgetBuilder, once per second,
util the countdown reaches zero.
TimerWidget.countdown(
reference: DateTime.now(),
builder: (BuildContext context, int seconds, bool isFinished) => ...;
)
Implementation
factory TimeBuilder.countdown({
Key? key,
required DateTime start,
required int seconds,
required CountdownWidgetBuilder builder,
}) {
return TimeBuilder(
//
key: key,
//
ifRebuilds: ({
required DateTime currentTime,
required DateTime? lastTime,
required int ticks,
}) {
return (lastTime == null || (currentTime.second != lastTime.second));
},
//
isFinished: ({
required DateTime currentTime,
required DateTime? lastTime,
required int ticks,
}) {
if (lastTime != null && (currentTime.second == lastTime.second)) return false;
int _seconds = (start.add(Duration(seconds: seconds))).difference(currentTime).inSeconds;
return _seconds <= 0;
},
//
builder: (
BuildContext context,
DateTime now,
int ticks,
bool isFinished,
) {
int _seconds = (start.add(Duration(seconds: seconds))).difference(now).inSeconds;
return builder(context, now, ticks, isFinished, countdown: _seconds);
},
//
);
}