If you want to create a countdown widget in flutter? It sounds like you are trying to show an animated text widget that changes over time. I would use an AnimatedWidget with a StepTween to ensure that the countdown only shows integer values.
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import 'package:flutter/material.dart'; void main() { runApp(new MaterialApp( home: new MyApp(), )); } class Countdown extends AnimatedWidget { Countdown({ Key key, this.animation }) : super(key: key, listenable: animation); Animation<int> animation; @override build(BuildContext context){ return new Text( animation.value.toString(), style: new TextStyle(fontSize: 150.0), ); } } class MyApp extends StatefulWidget { State createState() => new _MyAppState(); } class _MyAppState extends State<MyApp> with TickerProviderStateMixin { AnimationController _controller; static const int kStartValue = 4; @override void initState() { super.initState(); _controller = new AnimationController( vsync: this, duration: new Duration(seconds: kStartValue), ); } @override Widget build(BuildContext context) { return new Scaffold( floatingActionButton: new FloatingActionButton( child: new Icon(Icons.play_arrow), onPressed: () => _controller.forward(from: 0.0), ), body: new Container( child: new Center( child: new Countdown( animation: new StepTween( begin: kStartValue, end: 0, ).animate(_controller), ), ), ), ); } } |
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.