[Flutter]アニメーション終了後に処理をするには?

Flutter

どうも、ちょげ(@chogetarou)です。

AnimationControllerを使ったアニメーション終了後に処理をする方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

アニメーション終了後に処理をするには、AnimationControllerの「addStatusListener」を使います。

addStatusListenerは、アニメーションの状態が変わった時に呼び出される関数です。

このaddStatusListenerで、アニメーションが終わっている状態かを確認し、処理をします。

    _controller.addStatusListener((status) {
    //アニメーション終了後かの確認
      if (status == AnimationStatus.completed) {
        //アニメーション終了後の処理
      }
    });

addStatusListener内の関数の引数「status」は、アニメーションの状態を持っています。

addStatusListenerは、AnimationControllerの初期化後に設定します。

    _controller = AnimationController(
      duration: Duration(seconds: 3),
      vsync: this,
    );

    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        //アニメーション終了後の処理
      }
    });

まとめ

アニメーション終了後に処理をするには、AnimationControllerのaddStatusListenerを使います。

具体的には、addStatusListerの関数で、アニメーションの状態を確認し、終了後ならば処理をさせます。

コメント

タイトルとURLをコピーしました