首页 > 解决方案 > Flutter [animations] OpenContainer如何检测动画是否完成

问题描述

我正在使用OpenContainer animation打开一个屏幕,该屏幕可以在打开屏幕时显示警报对话框 - 屏幕尝试显示的项目的情况不再有效或被删除。

因为 OpenContainer 在动画期间渲染屏幕,所以会多次显示警告对话框。

我试图解决这个问题是修改OpenContainer buildPage方法以将动画状态返回给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 问题

标签: flutterflutter-animation

解决方案


I was able to do using the following code on the container for openBuilder.

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

see original answers - Flutter: Run method on Widget build complete


推荐阅读