教程 | 在 Flutter 应用里实现动画效果
如何在 Flutter 中实现动画效果。
本教程将讲解如何在 Flutter 中构建显式动画。这些示例相辅相成,来向你介绍动画库的不同方面。本教程以动画库中的基本概念、类和方法为基础,你可以在 动画介绍 中了解这些内容。
Flutter SDK 也内置了显式动画,比如
FadeTransition,SizeTransition
和 SlideTransition。这些 widget 接收一个 Animation 对象,通常由 AnimationController 驱动,并随着值得变化来更新 UI。它们比下面介绍的自定义显式动画更容易实现。
下面的内容会向你介绍几个动画示例。每个示例都提供了源代码的链接。
渲染动画
#
Animation 对象会随着时间生成数字序列。但屏幕上并未显示任何内容。要显示一个 Animation 对象,需将 Animation 对象存储为你的 widget 成员,然后用它的值来决定如何绘制。
参考下面的应用程序,它没有使用动画绘制 Flutter logo。
import 'package:material_ui/material_ui.dart';
void main() => runApp(const LogoApp());
class LogoApp extends StatefulWidget {
const LogoApp({super.key});
@override
State<LogoApp> createState() => _LogoAppState();
}
class _LogoAppState extends State<LogoApp> {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 10),
height: 300,
width: 300,
child: const FlutterLogo(),
),
);
}
}
源代码: animate0
下面的代码是加入动画效果的,logo 从无到全屏。当定义 AnimationController 时,必须要使用一个 vsync 对象。在 AnimationController 部分
会具体介绍 vsync 参数。
对比无动画示例,改动部分被突出显示:
class _LogoAppState extends State<LogoApp> {
class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin {
late Animation<double> animation;
late AnimationController controller;
@override
void initState() {
super.initState();
controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
);
animation = Tween<double>(begin: 0, end: 300).animate(controller)
..addListener(() {
setState(() {
// The state that has changed here is the animation object's value.
});
});
controller.forward();
}
@override
Widget build(BuildContext context) {
return Center(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 10),
height: 300,
width: 300,
height: animation.value,
width: animation.value,
child: const FlutterLogo(),
),
);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
}
源代码: animate1
通过 addListener() 注册的回调函数会调用 setState(),因此每次 Animation 生成一个新的数值,当前帧就被标记为 dirty(需要重建),使得 build() 再次被调用。在 build() 函数中,container 会改变大小,因为它的高和宽都读取 animation.value,而不是固定编码值。当 State 对象销毁时要清除控制器以防止内存溢出。
经过这些小改动,你成功创建了第一个 Flutter 动画。
使用 AnimatedWidget 进行简化
#
AnimatedWidget 基本类可以从动画代码中区分出核心 widget 代码。
AnimatedWidget 会自行管理其 State 对象和动画监听器,因此你无需在子类中实现它们。可以添加下面的 AnimatedLogo 类:
class AnimatedLogo extends AnimatedWidget {
const AnimatedLogo({super.key, required Animation<double> animation})
: super(listenable: animation);
@override
Widget build(BuildContext context) {
final animation = listenable as Animation<double>;
return Center(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 10),
height: animation.value,
width: animation.value,
child: const FlutterLogo(),
),
);
}
}
在绘制时,AnimatedLogo 会读取 animation 当前值。
LogoApp 持续控制 AnimationController 和 Tween,并将 Animation 对象传给 AnimatedLogo:
void main() => runApp(const LogoApp());
class AnimatedLogo extends AnimatedWidget {
const AnimatedLogo({super.key, required Animation<double> animation})
: super(listenable: animation);
@override
Widget build(BuildContext context) {
final animation = listenable as Animation<double>;
return Center(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 10),
height: animation.value,
width: animation.value,
child: const FlutterLogo(),
),
);
}
}
class LogoApp extends StatefulWidget {
// ...
}
class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin {
// ...
@override
void initState() {
super.initState();
controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
);
animation = Tween<double>(begin: 0, end: 300).animate(controller)
..addListener(() {
setState(() {
// The state that has changed here is the animation object's value.
});
});
animation = Tween<double>(begin: 0, end: 300).animate(controller);
controller.forward();
}
@override
Widget build(BuildContext context) {
return Center(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 10),
height: animation.value,
width: animation.value,
child: const FlutterLogo(),
),
);
}
Widget build(BuildContext context) => AnimatedLogo(animation: animation);
// ...
}
源代码: animate2
监听动画过程
#
了解动画何时改变状态通常是很有用的,比如完成,前进或后退。可以通过 addStatusListener() 来获得提示。下面是之前示例修改后的代码,这样就可以监听状态的改变和更新。修改部分会突出显示:
class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin {
late Animation<double> animation;
late AnimationController controller;
@override
void initState() {
super.initState();
controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
);
animation = Tween<double>(begin: 0, end: 300).animate(controller)
..addStatusListener((status) => debugPrint('$status'));
controller.forward();
}
// ...
}
运行这段代码,得到如下结果:
AnimationStatus.forward
AnimationStatus.completed
下一步,在起始或结束时,使用 addStatusListener() 反转动画。制造“呼吸”效果:
void initState() {
super.initState();
controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
);
animation = Tween<double>(begin: 0, end: 300).animate(controller);
animation = Tween<double>(begin: 0, end: 300).animate(controller)
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.reverse();
} else if (status == AnimationStatus.dismissed) {
controller.forward();
}
})
..addStatusListener((status) => debugPrint('$status'));
controller.forward();
}
源代码: animate3
使用 AnimatedBuilder 进行重构
#animate3 示例代码中有个问题,就是改变动画需要改变渲染 logo 的widget。较好的解决办法是,将任务区分到不同类里:
渲染 logo
定义动画对象
渲染过渡效果
你可以使用 AnimatedBuilder 类(或其他通用版本 ListenableBuilder)来完成分离。
AnimatedBuilder 是 widget 树的一个单独的 widget。与 AnimatedWidget 类似,都会自动监听来自 Animation 对象的通知,并在必要时在 widget 树中标记为已修改,所以这时不需要调用 addListener()。
应用于 animate4 示例的 widget 树长这样:

从 widget 树底部开始,渲染 logo 的代码很容易:
class LogoWidget extends StatelessWidget {
const LogoWidget({super.key});
// Leave out the height and width so it fills the animating parent.
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(vertical: 10),
child: const FlutterLogo(),
);
}
}
图表中间的三部分都是用 GrowTransition 中的 build() 方法创建的,如下。 GrowTransition widget 本身是无状态的,而且拥有定义过渡动画所需的一系列最终变量。
build() 方法会创建并返回 AnimatedBuilder,该方法将匿名 builder 回调函数和 LogoWidget 对象作为参数。渲染过渡效果实际上是在匿名 builder 回调函数中完成的,该回调函数会创建一个适当大小的 SizedBox(图中为 Container),强制 LogoWidget 配合缩小来适应该区域。
在下面这段代码中,一个比较棘手的问题是 child 看起来被指定了两次。其实是 child 的外部参照被传递给了 AnimatedBuilder,再传递给匿名闭包,然后用作 child 的对象。最终结果就是 AnimatedBuilder 被插入到 widget 树中的两个 widget 中间,且动画帧不会重建静态的 child 子树。
class GrowTransition extends StatelessWidget {
const GrowTransition({
required this.child,
required this.animation,
super.key,
});
final Widget child;
final Animation<double> animation;
@override
Widget build(BuildContext context) {
return Center(
child: AnimatedBuilder(
animation: animation,
builder: (context, child) {
return SizedBox(
height: animation.value,
width: animation.value,
child: child,
);
},
child: child,
),
);
}
}
最后,初始动画的代码看起来很像 animate2
的示例。
initState() 方法创建了 AnimationController 和 Tween,然后用 animate() 绑定它们。接下来的 build() 方法,它返回一个以 LogoWidget 为 child 的 GrowTransition 对象,和一个驱动过渡的动画对象。上面列出了三个主要因素。
void main() => runApp(const LogoApp());
class LogoWidget extends StatelessWidget {
const LogoWidget({super.key});
// Leave out the height and width so it fills the animating parent.
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(vertical: 10),
child: const FlutterLogo(),
);
}
}
class GrowTransition extends StatelessWidget {
const GrowTransition({
required this.child,
required this.animation,
super.key,
});
final Widget child;
final Animation<double> animation;
@override
Widget build(BuildContext context) {
return Center(
child: AnimatedBuilder(
animation: animation,
builder: (context, child) {
return SizedBox(
height: animation.value,
width: animation.value,
child: child,
);
},
child: child,
),
);
}
}
class LogoApp extends StatefulWidget {
// ...
}
class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin {
// ...
@override
Widget build(BuildContext context) => AnimatedLogo(animation: animation);
Widget build(BuildContext context) {
return GrowTransition(animation: animation, child: const LogoWidget());
}
// ...
}
源代码: animate4
同步动画
#
在这部分内容中,你会根据 监听动画过程 (animate3) 创建示例,该示例将使用 AnimatedWidget 持续进行动画。可以用在需要对透明度进行从透明到不透明动画处理的情况。
每个补间动画控制一个动画的不同方面,例如:
controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
);
sizeAnimation = Tween<double>(begin: 0, end: 300).animate(controller);
opacityAnimation = Tween<double>(begin: 0.1, end: 1).animate(controller);
通过 sizeAnimation.value 我们可以得到尺寸,通过 opacityAnimation.value 可以得到不透明度,不过,AnimatedLogo 已经监听到了控制器的动画,因此它可以直接根据该单一动画计算出这两个值。
修改 AnimatedLogo 来封装其 Tween 对象,以及其 build() 方法在母动画对象上调用
Tween.evaluate() 来计算所需的尺寸和不透明度值。下面的代码中将这些改动突出显示:
class AnimatedLogo extends AnimatedWidget {
const AnimatedLogo({super.key, required Animation<double> animation})
: super(listenable: animation);
// Make the tweens static because they don't change.
static final Tween<double> _opacityTween = Tween(begin: 0.1, end: 1);
static final Tween<double> _sizeTween = Tween(begin: 0, end: 300);
@override
Widget build(BuildContext context) {
final animation = listenable as Animation<double>;
return Center(
child: Opacity(
opacity: _opacityTween.evaluate(animation),
child: Container(
margin: const EdgeInsets.symmetric(vertical: 10),
height: _sizeTween.evaluate(animation),
width: _sizeTween.evaluate(animation),
child: const FlutterLogo(),
),
),
);
}
}
class LogoApp extends StatefulWidget {
const LogoApp({super.key});
@override
State<LogoApp> createState() => _LogoAppState();
}
class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin {
late Animation<double> animation;
late AnimationController controller;
@override
void initState() {
super.initState();
controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
);
animation = CurvedAnimation(parent: controller, curve: Curves.easeIn)
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.reverse();
} else if (status == AnimationStatus.dismissed) {
controller.forward();
}
});
controller.forward();
}
@override
Widget build(BuildContext context) => AnimatedLogo(animation: animation);
@override
void dispose() {
controller.dispose();
super.dispose();
}
}
源代码: animate5
后续
#This tutorial gives you a foundation for creating explicit animations in Flutter using tweens, but there are many other animation APIs to explore:
-
Implicit animations and
TweenAnimationBuilderfor simpler animations that don't require managing anAnimationController. -
Specialized
Tweensubclasses, such asColorTween,RectTween, andIntTween. - Staggered animations for coordinating sequential or overlapping motions.
- Hero animations for shared element transitions between routes.
-
Physics simulations and
fling()methods for motion modeled on real-world physics. -
ReverseAnimationand the Animations overview for a deeper look at Flutter's animation architecture.
除非另有说明,本文档之所提及适用于 Flutter 3.50 版本。本页面最后更新时间:2026-11-20。查看文档源码 或者 为本页面内容提出建议。