簡體   English   中英

Flutter [動畫] OpenContainer 如何檢測 animation 完成

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

我正在使用OpenContainer animation打開一個屏幕,該屏幕可以在打開屏幕時顯示警報對話框 - 屏幕嘗試顯示的項目的情況不再有效或被刪除。

由於 OpenContainer 在 animation 期間渲染屏幕,因此多次顯示警告對話框。

我試圖解決這個問題是修改OpenContainer buildPage方法以將 animation 狀態返回給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
        }
    });

每次 animation state 更改時都會調用此偵聽器。

希望這可以幫助!

我能夠在 openBuilder 的容器上使用以下代碼。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM