Flutter动画基础 #
一、动画概念 #
1.1 核心类 #
| 类 | 说明 |
|---|---|
| Animation | 动画值 |
| AnimationController | 动画控制器 |
| Tween | 值范围 |
二、基本动画 #
2.1 AnimationController #
dart
class _MyWidgetState extends State<MyWidget>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(seconds: 2),
vsync: this,
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}
2.2 Tween #
dart
Animation<double> animation = Tween<double>(
begin: 0,
end: 300,
).animate(_controller);
2.3 AnimatedWidget #
dart
class AnimatedBox extends AnimatedWidget {
AnimatedBox({required Animation<double> animation})
: super(listenable: animation);
@override
Widget build(BuildContext context) {
final animation = listenable as Animation<double>;
return Container(
width: animation.value,
height: animation.value,
color: Colors.blue,
);
}
}
三、总结 #
3.1 核心概念 #
| 概念 | 说明 |
|---|---|
| AnimationController | 控制动画 |
| Tween | 定义范围 |
| AnimatedWidget | 动画Widget |
3.2 下一步 #
让我们学习 自定义Widget!
最后更新:2026-03-28