首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Flutter 零基础入门(四十三):Flutter 动画基础 —— 动态交互与过渡效果

Flutter 零基础入门(四十三):Flutter 动画基础 —— 动态交互与过渡效果

作者头像
LarryLan
发布2026-03-05 17:24:37
发布2026-03-05 17:24:37
30
举报

Flutter 动画基础 —— 动态交互与过渡效果

到目前为止,你已经掌握了:

  • 多页面 App 架构与导航(TabBar / BottomNavigationBar / 命名路由)
  • 列表刷新与分页
  • 用户操作反馈(SnackBar / AlertDialog)
  • App 美化(主题、颜色、字体)

但是,一个静态页面的 App 很无趣。 Flutter 提供了丰富的动画组件,让 UI 更加生动。


一、Flutter 动画分类

类型

组件

说明

隐式动画

AnimatedContainer, AnimatedOpacity, AnimatedPadding, AnimatedAlign

属性变化自动过渡动画,无需 AnimationController

显式动画

AnimationController + Tween + AnimatedBuilder

完全可控动画,适合复杂效果

页面过渡

PageRouteBuilder, Hero

页面切换动画,元素过渡动画

本篇先从 隐式动画 入门。


二、AnimatedContainer

1️⃣ 基本用法

代码语言:javascript
复制
class AnimatedContainerDemo extends StatefulWidget{
@override
  _AnimatedContainerDemoState createState() => _AnimatedContainerDemoState();
}

class _AnimatedContainerDemoState extends State<AnimatedContainerDemo> {
double _width = 100;
double _height = 100;
  Color _color = Colors.blue;

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('AnimatedContainer 示例')),
      body: Center(
        child: AnimatedContainer(
          width: _width,
          height: _height,
          color: _color,
          duration: Duration(seconds: 1),
          curve: Curves.easeInOut,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _width = _width == 100 ? 200 : 100;
            _height = _height == 100 ? 200 : 100;
            _color = _color == Colors.blue ? Colors.orange : Colors.blue;
          });
        },
        child: Icon(Icons.play_arrow),
      ),
    );
  }
}

📌 功能解析:

  • AnimatedContainer 自动根据属性变化生成动画
  • duration → 动画持续时间
  • curve → 动画曲线
  • 点击 FloatingActionButton 切换状态 → 自动过渡动画

三、AnimatedOpacity

实现淡入淡出效果:

代码语言:javascript
复制
AnimatedOpacity(
  opacity: _visible ? 1.0 : 0.0,
  duration: Duration(seconds: 1),
  child: Container(
    width: 100,
    height: 100,
    color: Colors.green,
  ),
)

📌 配合 setState 切换 _visible,即可实现淡入淡出动画


四、AnimatedAlign

实现位置平滑移动:

代码语言:javascript
复制
AnimatedAlign(
  alignment: _align,
  duration: Duration(seconds: 1),
  child: FlutterLogo(size: 80),
)
onPressed: () {
  setState(() {
    _align = _align == Alignment.topLeft ? Alignment.bottomRight : Alignment.topLeft;
  });
}

📌 平滑移动元素到不同位置


五、显式动画入门

1️⃣ AnimationController + Tween + AnimatedBuilder

代码语言:javascript
复制
class ScaleAnimationDemo extends StatefulWidget {
  @override
  _ScaleAnimationDemoState createState() => _ScaleAnimationDemoState();
}

class _ScaleAnimationDemoState extends State<ScaleAnimationDemo> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
    );
    _animation = Tween<double>(begin: 0, end: 1).animate(_controller);
    _controller.forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('显式动画示例')),
      body: Center(
        child: AnimatedBuilder(
          animation: _animation,
          builder: (context, child) {
            return Transform.scale(
              scale: _animation.value,
              child: FlutterLogo(size: 100),
            );
          },
        ),
      ),
    );
  }
}

📌 功能解析:

  • AnimationController 控制动画时长与播放
  • Tween 设置动画开始/结束值
  • AnimatedBuilder 构建动画 UI

六、页面过渡动画

1️⃣ Hero 动画

代码语言:javascript
复制
Hero(
  tag: 'logo',
  child: FlutterLogo(size: 80),
)

在跳转页面也使用相同 tag,即可实现元素平滑过渡

2️⃣ PageRouteBuilder 自定义页面切换

代码语言:javascript
复制
Navigator.push(context, PageRouteBuilder(
  pageBuilder: (context, animation, secondaryAnimation) => DetailPage(),
  transitionsBuilder: (context, animation, secondaryAnimation, child) {
    return FadeTransition(opacity: animation, child: child);
  },
));

📌 页面切换时淡入淡出效果


七、常见坑

❌ 忘记 dispose AnimationController → 内存泄漏 ❌ duration 设置为 0 → 动画无效 ❌ AnimatedBuilder 未传 animation → 不会刷新 ❌ Hero tag 冲突 → 页面过渡异常

📌 建议:

  • 动画组件尽量使用隐式动画简单快速
  • 复杂效果用显式动画可控
  • Hero / PageRouteBuilder 用于页面切换过渡

八、本篇你真正掌握了什么?

你已经学会:

  • AnimatedContainer、AnimatedOpacity、AnimatedAlign 隐式动画
  • AnimationController + Tween + AnimatedBuilder 显式动画
  • Hero 动画与页面过渡效果
  • 点击 / 页面切换动画实现 UI 生动交互

📌 到这里为止:

你的 App 已经具备基本动画交互能力


九、一句话总结

隐式动画快速简单 显式动画精确可控 Hero / PageRouteBuilder 页面过渡动画 App 更生动、更有交互感


🔜 下一篇预告

《Flutter 零基础入门(四十四):Flutter 图标与图片资源管理 —— 视觉元素实战》

下一篇我们将学习:

  • 使用 Flutter 内置图标
  • 自定义图片资源
  • 图片 / 图标加载与优化
  • App 视觉效果增强

🚀 让你的 App 更加美观生动

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2026-03-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Larry的Hub 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Flutter 动画基础 —— 动态交互与过渡效果
    • 一、Flutter 动画分类
    • 二、AnimatedContainer
      • 1️⃣ 基本用法
    • 三、AnimatedOpacity
    • 四、AnimatedAlign
    • 五、显式动画入门
      • 1️⃣ AnimationController + Tween + AnimatedBuilder
    • 六、页面过渡动画
      • 1️⃣ Hero 动画
      • 2️⃣ PageRouteBuilder 自定义页面切换
    • 七、常见坑
    • 八、本篇你真正掌握了什么?
    • 九、一句话总结
    • 🔜 下一篇预告
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档