[Flutter]アニメーションの状態を取得するには?

Flutter

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

アニメーションの状態を取得するには、どうしたらいいのでしょうか?

スポンサーリンク

方法

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

アニメーションの状態を取得するには、AnimationControllerの「status」プロパティを使います。

この「status」プロパティにアクセスすることで、アニメーションの状態を取得することが出来ます。

    _controller = AnimationController(
      duration: Duration(milliseconds: 5),
      vsync: this,
    );

_controller.status //アニメーションの状態を取得

アニメーションの状態には、次の2つがあります。

  • AnimationStatus.completed : アニメーション終了後
  • AnimationStatus.dismissed : アニメーションする前

使用例

  void _iconChange() {
    if (_controller.status == AnimationStatus.completed) {
      _controller.reverse();
    } else {
      _controller.forward();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
        child: AnimatedBuilder(
            animation: _controller,
            child: IconButton(
              onPressed: _iconChange,
              icon: Icon(Icons.favorite),
              color: _colorAnimation.value,
            ),
            builder: (BuildContext context, child) {
              return child!;
            }));
  }

コメント

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