Flutter [animations] OpenContainer如何检测动画结束

Flutter [animations] OpenContainer how to detect the animation is finished

我正在使用 OpenContainer animation 打开一个屏幕,该屏幕可以在打开屏幕时显示警告对话框 - 屏幕试图显示的项目的大小写不再有效或已删除。

由于 OpenContainer 在动画期间渲染屏幕,因此会多次显示警告对话框。

我尝试 解决此问题是将 OpenContainer buildPage 方法修改为 return 动画状态到 openBuilder 回调。有没有不修改 OpenContainer 代码的更好方法?

child: AnimatedBuilder(
        animation: animation,
        builder: (BuildContext context, Widget child) {
          if (animation.isCompleted) {
            return SizedBox.expand(
              child: Material(
                color: openColor,
                elevation: openElevation,
                shape: openShape,
                child: Builder(
                  key: _openBuilderKey,
                  builder: (BuildContext context) {
                    return openBuilder(context, closeContainer, false); // added false
                  },
                ),
              ),
            );
          }

重现问题的代码 - https://gist.github.com/MartinJLee/0992a986ad641ef5b4f477fb1ce69249

你可以像这样给你的 AnimationController 添加一个监听器

假设你有一个像这样的 AnimationController -

AnimationController _animationController =  AnimationController(
            vsync: this, 
            duration: Duration(seconds: 5),
      );

然后你可以使用 addStatusListener 方法向这个 _animationController 添加一个状态监听器,就像这样 -

_animationController.addStatusListener((status) {
        if(status == AnimationStatus.completed){
          //Do something here
        }
    });

每次动画状态改变时都会调用此侦听器。

希望对您有所帮助!

我能够在 openBuilder 的容器上使用以下代码。

  void initState() {
    super.initState();
    WidgetsBinding.instance
        .addPostFrameCallback((_) => yourFunction(context));
  }

查看原答案 - Flutter: Run method on Widget build complete