TweenAnimationBuilder和AnimatedBuilder两种动画的比较

TweenAnimationBuilderAnimatedBuilder 比较

基本概念

TweenAnimationBuilder

  • ImplicitlyAnimatedWidget 的子类
  • 提供了一种简单的方式来创建基于 Tween 的隐式动画
  • 适用于需要根据数值变化进行动画的场景,如颜色、大小、透明度或位置等过渡效果

AnimatedBuilder

  • AnimatedWidget 的子类
  • 一个强大的动画工具类,用于将动画逻辑与UI组件分离
  • 主要用于显式动画(需要手动创建 AnimationController

主要区别

特性 TweenAnimationBuilder AnimatedBuilder
使用方式 隐式动画,自动处理动画 显式动画,需手动控制
动画控制器 内部自动管理 需要手动创建和管理 AnimationController
代码复杂度 简单,适合快速实现 相对复杂,但更灵活
动画触发 设置目标值后自动动画 通过调用 AnimationController 方法触发
性能优化 适合简单的动画场景 可以更好地分离动画与UI部分

使用示例

TweenAnimationBuilder 示例

dart 复制代码
TweenAnimationBuilder<double>(
  duration: Duration(seconds: 1),
  tween: Tween<double>(begin: 0, end: 100),
  builder: (BuildContext context, double value, Widget? child) {
    return Container(
      width: value,
      height: value,
      color: Colors.blue,
    );
  },
)

AnimatedBuilder 示例

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

class _MyAnimationState extends State<MyAnimation>
    with TickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

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

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animation,
      builder: (BuildContext context, Widget? child) {
        return Container(
          width: _animation.value,
          height: _animation.value,
          color: Colors.blue,
        );
      },
    );
  }

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

性能优化技巧

对于两者都有一个重要的优化技巧:如果 builder 中有不参与动画的组件,应该将其放在 child 参数中,这样可以避免不必要的重建:

dart 复制代码
TweenAnimationBuilder<double>(
  duration: Duration(seconds: 1),
  tween: Tween<double>(begin: 0, end: 100),
  child: Icon(Icons.star, size: 50), // 不参与动画的部分
  builder: (BuildContext context, double value, Widget? child) {
    return Container(
      width: value,
      height: value,
      color: Colors.blue,
      child: child, // 将不变的widget传递进来
    );
  },
)

选择建议

  • 使用 TweenAnimationBuilder 当你需要:

    • 快速实现简单的动画效果
    • 不想手动管理 AnimationController
    • 实现隐式动画(如点击后自动动画到目标值)
  • 使用 AnimatedBuilder 当你需要:

    • 更复杂的动画控制
    • 精确控制动画的开始、暂停、反向等
    • 在动画过程中与其他组件交互
    • 需要将动画逻辑与UI组件分离

总的来说,TweenAnimationBuilder 更适合简单、自动化的动画,而 AnimatedBuilder 提供了更多的灵活性和控制能力,适合复杂的动画场景。

相关推荐
maaath1 天前
【maaath】Flutter for OpenHarmony 跨平台工程集成密码加密能力
flutter·华为·harmonyos
yeziyfx1 天前
Flutter 纯色矩形
flutter
liulian09161 天前
Flutter for OpenHarmony 混合开发实践:用户反馈功能的实现与适配
flutter·华为·学习方法·harmonyos
Hello__77771 天前
开源鸿蒙 Flutter 实战|文章分类标签功能全流程实现
flutter·开源·harmonyos
xiaoyan20151 天前
2026爆肝!Flutter3.41纯手撸微信聊天APP原生应用
android·flutter·dart
魔士于安1 天前
Unity UI图片 复活节UI,卡通风格
游戏·ui·unity·游戏引擎·材质·贴图
程序员老刘1 天前
当全网都在喊“程序员要被AI取代了”,Flutter给了另一种答案
flutter·ai编程·客户端
国医中兴1 天前
Flutter 三方库 nhost_graphql_adapter 的鸿蒙化适配指南 - 云端数据实时对齐、GraphQL 架构实战、鸿蒙级全栈交互专家
flutter·harmonyos·graphql
for_ever_love__1 天前
UI学习:UITableView的基本操作及折叠cell
学习·ui·ios
IntMainJhy1 天前
Flutter 三方库 get_it + flutter_bloc 的鸿蒙化适配与实战指南
flutter·华为·harmonyos