I am a beginner in flutter and I wanted to implement a simple CountDown Time (that counts in minutes and seconds) in my quiz app.
I tried to implement the CountdownTimer constructor I found in the official Flutter doc site, but I couldn't apply the constructor into real code in my application. I was able to copy the code provided by @Yann39 at (Flutter Countdown Timer) which worked. But I would like to learn how to implement the constructor myself.
Timer _timer;
int _start = 10;
void startTimer() {
const oneSec = const Duration(seconds: 1);
_timer = new Timer.periodic(
oneSec,
(Timer timer) => setState(() {
if (_start < 1) {
timer.cancel();
} else {
_start = _start - 1;
}
}));
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(title: Text("Timer test")),
body: Column(
children: <Widget>[
RaisedButton(
onPressed: () {
startTimer();
},
child: Text("start"),
),
Text("$_start")
],
));
}
CountdownTimer constructor
CountdownTimer(
Duration duration,
Duration increment, {
Stopwatch stopwatch
})
I basically want to know how to implement the CountDown timer constructor to create my code. I believe knowing this will also help me implement and interpret other constructors I encounter.