首先,需要确保AnimatedSwitcher包含两个或更多的子控件,并且它们的key属性不同。其次,需要在AnimatedSwitcher中设置transitionBuilder属性以实现动画效果。
示例代码如下:
AnimatedSwitcher(
duration: const Duration(milliseconds: 500),
transitionBuilder: (Widget child, Animation animation) {
return ScaleTransition(
child: child,
scale: animation,
);
},
child: // 第一个子控件,
secondChild: // 第二个子控件,
switchInCurve: Curves.easeIn,
switchOutCurve: Curves.easeOut
)
在上面的代码中,使用了ScaleTransition来作为AnimatedSwitcher的动画效果。可以使用其他内置的动画效果,如FadeTransition或SlideTransition,也可以自定义动画效果。另外,transitionBuilder可以带有两个参数:child和animation,其中child是当前子控件,而animation则是执行动画的对象。