简体   繁体   English

Flutter - 倒数计时器的更新持续时间

[英]Flutter - Update duration of countdown timer

I am trying to show countdown timer using this resource: https://stackoverflow.com/a/64312145/13240914 :我正在尝试使用此资源显示倒数计时器: 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.例如,当用户单击一个按钮时,持续时间将从 10 秒变为 20 秒,但持续时间根本不会变为 20 秒……它会一直计数 10 秒。 Is there a way to update the value of Duration when the duration is still running?有没有办法在持续时间仍在运行时更新 Duration 的值?

you can't jump on a new value using TweenAnimationBuilder.您不能使用 TweenAnimationBuilder 跳转到新值。 you have to use explicit animation for this.为此,您必须使用明确的 animation 。 check this out: How to choose which Flutter Animation Widget is right for you?看看这个: 如何选择适合您的 Flutter Animation Widget?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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