Flutter - Create a countdown widget

前端 未结 5 1916
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 20:58

I am trying to build a countdown widget. Currently, I got the structure to work. I only struggle with the countdown itself. I tried this approach using the countdown plugin:

5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-05 21:58

    Why not use a simple TweenAnimationBuilder its easy to use and you don't need to manage any stream controllers or worry about using streams and disposing them off etc;

    TweenAnimationBuilder(
                    duration: Duration(seconds: 10),
                    tween: Tween(begin: 100.0, end: 0.0),
                    onEnd: () {
                      print('Countdown ended');
                    },
                    builder: (BuildContext context, double value, Widget child) {
                      return Padding(
                          padding: const EdgeInsets.symmetric(vertical: 5),
                          child: Text('${value.toInt()}',
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                  color: Colors.black,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 40)));
                    }),
    

    here's the dartpad example to playaround output:

    originally answered here

提交回复
热议问题