Want to add Countdown Timer in Flutter app? You can use the CountdownTimer class from the quiver.async library, usage is even simpler:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import 'package:quiver/async.dart'; [...] int _start = 10; int _current = 10; void startTimer() { CountdownTimer countDownTimer = new CountdownTimer( new Duration(seconds: _start), new Duration(seconds: 1), ); var sub = countDownTimer.listen(null); sub.onData((duration) { setState(() { _current = _start - duration.elapsed.inSeconds; }); }); sub.onDone(() { print("Done"); sub.cancel(); }); } Widget build(BuildContext context) { return new Scaffold( appBar: AppBar(title: Text("Timer test")), body: Column( children: <Widget>[ RaisedButton( onPressed: () { startTimer(); }, child: Text("start"), ), Text("$_current") ], ), ); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.