flutter - 在屏幕上显示“简单持续时间”计时器

标签 flutter dart flutter-animation

有什么简单的方法可以在屏幕上显示DURATION中的剩余时间?

void onTapDown(TapDownDetails details){
        if(state == State.menu){
           timer = Timer(duration = new Duration(seconds: 5), () {
            state = State.playing;
           });
           print("STARTING IN [secondsremaing]");
        }
还是我必须使其变得复杂并实现其他任何类?

最佳答案

这将无法正常工作,因为 Timer 仅会在给定的Duration之后调用给定的回调,但不会为您打勾
如果要显示指示剩余时间的更新的小部件,则必须使用自动收录器,通常可以通过设置 AnimationController 来实现。

在您的情况下,可能看起来像这样(假设您使用的是StatefulWidget):

class _YourWidgetsState extends State<YourWidget> with SingleTickerProviderMixin {
  AnimationController remainingTimeController;

  @override
  void initState() {
    super.initState();

    remainingTimeController = AnimationController(vsync: this, duration: const Duration(seconds: 5));
  }

  /// Used somewhere in your build method.
  void onTapDown(TapDownDetails details) {
    if(state == State.menu){
      timer = Timer(duration = new Duration(seconds: 5), () {
        state = State.playing;
      });
      print("STARTING IN [secondsremaing]");
    }
  }

  @override
  Widget build(BuildContext context) {
    // Obviously return your other widgets in here as well.

    return AnimatedBuilder(
      animation: remainingTimeController,
      builder: (context, _) => Text('${remainingTimeController.value * 60 * 5}'),
    );
  }
}

关于flutter - 在屏幕上显示“简单持续时间”计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59589826/

相关文章:

firebase - 检测人脸和裁剪人脸图像 flutter firebase ml

flutter - dart - 具有属性的接口(interface)

PageView 中类似 Flutter Hero 的过渡

Flutter DropDownButton Popup 按钮下方 200px

flutter - 布局更改时行到列的动画转换

动画完成后 flutter 反向动画不起作用

flutter - Android Studio 选项卡前景不会消失

flutter - FutureBuilder返回传递给var Flutter

flutter - 像工厂一样使用 Provider

proxy - 如何使用 dart 实现委托(delegate)/代理?