dart - 如何在动画中一个接一个地显示动画

标签 dart flutter flutter-animation

我已经将数组映射到一个类,并且正在相应地显示动画,但是动画可一次用于整个对象。我希望在后续动画之间有一个延迟。

The screenshot of the game is here. The stars should appear with animation one after the other我正在使用的代码是:

for (var i = 0; i < 4; i++) {
  myScore > (10*i) ? stars.add("true") : stars.add("false");      
}

Widget _buildItem(int index, String text) {
return new MyButton(
    key: new ValueKey<int>(index),
    text: text,
    keys: keys++,
    onPress: () {

    }
    );

}
    class MyButton extends StatefulWidget {

      MyButton(
          {Key key,
          this.text,
          this.keys,
          this.onPress})
          : super(key: key);
      final String text;
      final VoidCallback onPress;
      int keys;
      @override
      _MyButtonState createState() => new _MyButtonState();
    }

class _MyButtonState extends State<MyButton> with TickerProviderStateMixin {
  AnimationController controller;
  Animation<double> animation;
  String _displayText;

  initState() {
    super.initState();
    print("_MyButtonState.initState: ${widget.text}");
    _displayText = widget.text;

    controller = new AnimationController(
        duration: new Duration(milliseconds: 600), vsync: this);

    animation = new CurvedAnimation(parent: controller, curve: Curves.easeIn)
      ..addStatusListener((state) {
//        print("$state:${animation.value}");
        if (state == AnimationStatus.dismissed) {
          print('dismissed');
          if (widget.text != null) {
            setState(() => _displayText = widget.text);
            controller.forward();
          }
        }
      });
  }

   @override
    void didUpdateWidget(MyButton oldWidget) {
      super.didUpdateWidget(oldWidget);
     // sleep(const Duration(microseconds: 10)); //I tried adding a delay here but instead it delays the entire thing.
    }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Size media = MediaQuery.of(context).size;
    double ht = media.height;
    double wd = media.width;
    widget.keys++;
    print("_MyButtonState.build");    
    return new Shake(
      animation: animation,
      child: new GestureDetector(

     child: new Container(

     child: new FlatButton(
         onPressed: () => widget.onPress(),
         color: Colors.transparent,

         child: new Icon(
        _displayText == "true" ? Icons.star : Icons.star_border,
        key: new Key("${widget.keys}"),
        size: ht > wd ? ht * 0.05 : wd * 0.05,
        color: Colors.black,
         )

    ),)
    ) );
  }
}

最佳答案

使用Flutter中的交错动画进行一系列操作,而不是一次执行所有操作。按照Staggered Animations链接使用Flutter构建交错动画。

关于dart - 如何在动画中一个接一个地显示动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50604058/

相关文章:

flutter - 如何在其他小部件上方绘制 InkResponse?

flutter - 如果 parent 更新,如何避免重建 child

PageView 中类似 Flutter Hero 的过渡

flutter - 如何检查 Flutter 应用程序是否正在调试中运行?

flutter - Map的Cloud Firestore查询在Flutter/Dart中返回空快照

android - Flutter:如何构建改变移动设备方向时移动 block 的逻辑?

Flutter - 在导航期间创建固定 AppBar 的最佳实践是什么

dart - 创建并写入具有 'execute' 权限的文件?

rest - 使用运行中的休息服务器调试Dart应用程序

flutter - 如何构建应用程序状态以便所有相关的小部件在状态更改时更新?