0
0

More than 1 year has passed since last update.

AnimatedSwitcherが動かない!というときの対処法

Posted at

結論

これを

class _BadWidget extends ConsumerWidget {
  const _BadWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return AnimatedSwitcher(
      duration: const Duration(milliseconds: 100),
      child: Icon(
        ref.watch(_shouldShowDetail)
            ? Icons.keyboard_arrow_up
            : Icons.keyboard_arrow_down,
      ),
    );
  }
}

こうする

class _GoodWidget extends ConsumerWidget {
  const _GoodWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return AnimatedSwitcher(
      duration: const Duration(milliseconds: 100),
      child: ref.watch(_shouldShowDetail)
          ? Icon(
              Icons.keyboard_arrow_up,
              key: UniqueKey(),
            )
          : Icon(
              Icons.keyboard_arrow_down,
              key: UniqueKey(),
            ),
    );
  }
}

参考

If the "new" child is the same widget type and key as the "old" child, but with different parameters, then AnimatedSwitcher will not do a transition between them, since as far as the framework is concerned, they are the same widget and the existing widget can be updated with the new parameters.

要約すると、 新しいウィジェットが古いウィジェットと同じキーならパラメータ違くても同じWidgetとして認識するからアニメーションしないよ という感じです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0