結論
これを
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として認識するからアニメーションしないよ という感じです。