在AnimatedSize的父部件添加动画控制器并在onPressed方法中调用控制器的forward方法,以便启动AnimatedSize的缓动动画。
示例代码:
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State with TickerProviderStateMixin {
AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 1),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return InkWell(
child: AnimatedSize(
duration: Duration(seconds: 1),
vsync: this,
child: Text('Hello, world!'),
),
onTap: () {
_controller.forward();
},
);
}
}