简体   繁体   中英

Flutter - Update duration of countdown timer

I am trying to show countdown timer using this resource: https://stackoverflow.com/a/64312145/13240914 :

TweenAnimationBuilder<Duration>(
  duration: Duration(minutes: 3),
  tween: Tween(begin: Duration(minutes: 3), end: Duration.zero),
  onEnd: () {
    print('Timer ended');
  },
  builder: (BuildContext context, Duration value, Widget? child) {
    final minutes = value.inMinutes;
    final seconds = value.inSeconds % 60;
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 5),
      child: Text('$minutes:$seconds',
               textAlign: TextAlign.center,
               style: TextStyle(
               color: Colors.black,
               fontWeight: FontWeight.bold,
               fontSize: 30)));
    }),

The problem is when duration is still running, I would like to update duration. For example when user click a button, the duration will change from 10 to 20 seconds, but the duration doesn't change to 20 seconds at all... it keeps going to count 10 seconds. Is there a way to update the value of Duration when the duration is still running?

you can't jump on a new value using TweenAnimationBuilder. you have to use explicit animation for this. check this out: How to choose which Flutter Animation Widget is right for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM