Flutter动画系统详解

一、动画系统核心概念

1. 动画类型

  • 隐式动画 :通过 AnimatedFoo 组件自动处理动画

  • 显式动画 :通过 AnimationController 手动控制动画

2. 核心类

Dart 复制代码
// 主要动画类
Animation<T>        // 动画值容器
AnimationController // 动画控制器
Tween<T>           // 定义动画值范围
Curve             // 定义动画曲线

二、隐式动画(最简单)

示例:基本隐式动画组件

Dart 复制代码
// AnimatedContainer - 自动动画化属性变化
AnimatedContainer(
  duration: Duration(seconds: 1),
  width: _selected ? 200.0 : 100.0,
  height: _selected ? 200.0 : 100.0,
  color: _selected ? Colors.red : Colors.blue,
)

// AnimatedOpacity - 透明度动画
AnimatedOpacity(
  opacity: _visible ? 1.0 : 0.0,
  duration: Duration(milliseconds: 500),
)

// AnimatedSwitcher - 子组件切换动画
AnimatedSwitcher(
  duration: Duration(milliseconds: 500),
  child: _showFirst 
    ? Text("First", key: ValueKey(1))
    : Text("Second", key: ValueKey(2)),
)

三、显式动画(更灵活)

1. 基本显式动画流程

Dart 复制代码
class MyAnimation extends StatefulWidget {
  @override
  _MyAnimationState createState() => _MyAnimationState();
}

class _MyAnimationState extends State<MyAnimation> 
    with SingleTickerProviderStateMixin {
  
  AnimationController _controller;
  Animation<double> _animation;
  
  @override
  void initState() {
    super.initState();
    
    // 1. 创建动画控制器
    _controller = AnimationController(
      duration: Duration(seconds: 2),
      vsync: this,  // 需要混入 SingleTickerProviderStateMixin
    );
    
    // 2. 创建动画曲线
    final curve = CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    );
    
    // 3. 创建 Tween 和动画
    _animation = Tween<double>(
      begin: 0.0,
      end: 300.0,
    ).animate(curve);
    
    // 4. 添加状态监听
    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        _controller.forward();
      }
    });
  }
  
  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animation,
      builder: (context, child) {
        return Container(
          width: _animation.value,
          height: _animation.value,
          color: Colors.blue,
        );
      },
    );
  }
  
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}

2. 使用 AnimationController 方法

Dart 复制代码
// 启动动画
_controller.forward();    // 正向播放
_controller.reverse();    //反向播放
_controller.reset();      // 重置
_controller.stop();       // 停止
_controller.repeat();     // 重复播放

// 控制动画
_controller.animateTo(0.5); // 动画到特定值

四、常用动画组件

1. Hero 动画(页面切换动画)

Dart 复制代码
// 页面 A
Hero(
  tag: 'image-hero',
  child: Image.asset('images/flutter.png'),
  flightShuttleBuilder: (flightContext, animation, direction, fromContext, toContext) {
    return ScaleTransition(
      scale: animation.drive(Tween(begin: 0.0, end: 1.0)),
      child: toContext.widget,
    );
  },
)

// 页面 B(使用相同 tag)
Hero(
  tag: 'image-hero',
  child: Image.asset('images/flutter.png'),
)

2. 交错动画 (Staggered Animation)

Dart 复制代码
// 创建多个动画序列
_controller = AnimationController(
  duration: Duration(milliseconds: 2000),
  vsync: this,
);

// 定义多个动画
_widthAnimation = Tween<double>(
  begin: 50,
  end: 200,
).animate(CurvedAnimation(
  parent: _controller,
  curve: Interval(0.0, 0.5), // 在 0-50% 的时间段执行
));

_heightAnimation = Tween<double>(
  begin: 50,
  end: 200,
).animate(CurvedAnimation(
  parent: _controller,
  curve: Interval(0.5, 1.0), // 在 50-100% 的时间段执行
));
相关推荐
微祎_31 分钟前
Flutter for OpenHarmony:构建一个 Flutter 平衡球游戏,深入解析动画控制器、实时物理模拟与手势驱动交互
flutter·游戏·交互
ZH15455891312 小时前
Flutter for OpenHarmony Python学习助手实战:面向对象编程实战的实现
python·学习·flutter
renke33642 小时前
Flutter for OpenHarmony:构建一个 Flutter 色彩调和师游戏,RGB 空间探索、感知色差计算与视觉认知训练的工程实现
flutter·游戏
王码码20353 小时前
Flutter for OpenHarmony 实战之基础组件:第三十一篇 Chip 系列组件 — 灵活的标签化交互
android·flutter·交互·harmonyos
ujainu4 小时前
Flutter + OpenHarmony 实现经典打砖块游戏开发实战—— 物理反弹、碰撞检测与关卡系统
flutter·游戏·openharmony·arkanoid·breakout
微祎_4 小时前
构建一个 Flutter 点击速度测试器:深入解析实时交互、性能度量与响应式 UI 设计
flutter·ui·交互
王码码20354 小时前
Flutter for OpenHarmony 实战之基础组件:第二十七篇 BottomSheet — 动态底部弹窗与底部栏菜单
android·flutter·harmonyos
ZH15455891315 小时前
Flutter for OpenHarmony Python学习助手实战:Web开发框架应用的实现
python·学习·flutter
晚霞的不甘5 小时前
Flutter for OpenHarmony 构建简洁高效的待办事项应用 实战解析
flutter·ui·前端框架·交互·鸿蒙
百锦再5 小时前
Vue高阶知识:利用 defineModel 特性开发搜索组件组合
前端·vue.js·学习·flutter·typescript·前端框架