【动画 widget】Flutter TweenAnimationBuilder

简介: 【动画 widget】Flutter TweenAnimationBuilder

image.png

大家好,我是 17,今天的每日 widget 为大家介绍 TweenAnimationBuilder。

Flutter TweenAnimationBuilder 继承自 ImplicitlyAnimatedWidget,它的作用是生成一个有动画功能的 StatefulWidget widget 作为复杂 widget 的一部分。

源码分析

构造函数

const TweenAnimationBuilder({
    super.key,
    required this.tween,
    required super.duration,
    super.curve,
    required this.builder,
    super.onEnd,
    this.child,
  })
复制代码

ImplicitlyAnimatedWidget 相比,增加了 tween 和 build。 tween 只有一个,不像   ImplicitlyAnimatedWidget 那样可以操作 多个 tween。

if (_currentTween!.begin != _currentTween!.end) {
      controller.forward();
 }
复制代码

ImplicitlyAnimatedWidget 不同的是,一旦发现 tween 的开始结束不一样,就立即执行动画,不用等到通过改变属性来触发。

Widget build(BuildContext context) {
    return widget.builder(context, _currentTween!.evaluate(animation), widget.child);
  }
复制代码

widget.builder 通过一个函数生成 widget,给我们可以自定义 widget 内容的能力。

@override
  void forEachTween(TweenVisitor<dynamic> visitor) {
    assert(
      widget.tween.end != null,
      'Tween provided to TweenAnimationBuilder must have non-null Tween.end value.',
    );
    _currentTween = visitor(_currentTween, widget.tween.end, (dynamic value) {
      assert(false);
      throw StateError('Constructor will never be called because null is never provided as current tween.');
    }) as Tween<T>?;
  }
复制代码

TweenAnimationBuilder 为我们复写了 forEachTween。 参数 tween.end 不能为空,实际上 ,tween.begin 也不能为空,在 initState 阶段,已经给 tween.begin 赋值。这样一来,tween 的值就是完整的。和 ImplicitlyAnimatedWidget 出场时没有动画相比, 只要 tween.begin!=tween.end 就会有出场动画。

tween.end 不为空。所以就不需要 tween 的构造函数了,visitor 中的构造函数永远不会执行。

使用 TweenAnimationBuilder

使用 TweenAnimationBuilder 要比 使用 ImplicitlyAnimatedWidget 简单的多,不需要自定义类。

还是拿上次 AnimatedWidget 正方形 的例子,看看用 TweenAnimationBuilder 如何写。

class MyAnimation extends StatefulWidget {
  const MyAnimation({super.key});
  @override
  State<MyAnimation> createState() => _MyAnimationState();
}
class _MyAnimationState extends State<MyAnimation> {
  double targetValue = 100;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Center(
                child: TweenAnimationBuilder<double>(
      tween: Tween<double>(begin: 0, end: targetValue),
      builder: (context, value, child) {
        return GestureDetector(
          onTap: () {
              setState(() {
              targetValue = targetValue == 100 ? 50 : 100;
            });
          },
          child: Container(color: Colors.blue[200],width: value,height: value,),
        );
      },
      duration:const Duration(seconds: 1),
    ))));
  }
}
复制代码

效果上,动画并不能循环播放。只能是出场的时候执行一次,受到点击的时候再执行一次

和用 ImplicitlyAnimatedWidget 的实现方式相比,代码确实简洁很多。TweenAnimationBuilder 让动画 widget 以内嵌的方式嵌入到其它 widget 当中,省去了自定类。

但是 TweenAnimationBuilder 也是有他的不足的,如果要多次复用动画 Widget ,还是用直接继承 AnimatedWidget 或  juejin.cn/post/717196… 的方式比较好,并且 TweenAnimationBuilder 只能有一个 tween。

性能优化

TweenAnimationBuilder 的优化方式和 AnimatedBuilder 一样。已经说过了,不再赘述。

目录
相关文章
|
8天前
|
前端开发
Flutter快速实现自定义折线图,支持数据改变过渡动画
Flutter快速实现自定义折线图,支持数据改变过渡动画
19 4
Flutter快速实现自定义折线图,支持数据改变过渡动画
|
1月前
Flutter-实现头像叠加动画效果
Flutter-实现头像叠加动画效果
24 0
|
1月前
Flutter-加载中动画
Flutter-加载中动画
16 0
|
1月前
Flutter-自定义表情雨下落动画
Flutter-自定义表情雨下落动画
15 0
|
1月前
Flutter-数字切换动画
Flutter-数字切换动画
11 0
|
1月前
|
开发者
Flutter 动画学习
Flutter 动画学习
|
3月前
|
数据库 Android开发
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
|
3月前
|
前端开发 开发者 UED
【Flutter前端技术开发专栏】Flutter中的动画与过渡效果实现
【4月更文挑战第30天】Flutter UI框架以其高性能动画库著称,允许开发者轻松创建复杂动画。动画基于`Animation&lt;double&gt;`类,结合`Tween`、`Curve`和`AnimationController`实现。简单示例展示了一个点击按钮后放大效果的创建过程。此外,Flutter提供预定义动画组件和`Navigator`类实现页面过渡。`PageRouteBuilder`允许自定义过渡,而`Hero`动画则实现跨页面的平滑过渡。借助这些工具,开发者能提升应用的视觉吸引力和交互体验。
80 0
【Flutter前端技术开发专栏】Flutter中的动画与过渡效果实现
|
3月前
|
开发框架 API 开发者
Flutter的动画:实现方式与动画库的技术探索
【4月更文挑战第26天】探索Flutter动画机制与库:基础动画、自定义动画、物理动画及Lottie、AnimatedWidgets、EasyAnimations等库的应用,助开发者实现丰富动画效果,提升用户体验。同时,了解性能优化技巧,如避免重绘、利用离屏渲染和GPU加速,确保动画流畅。 Flutter为移动应用开发带来强大动画支持。
|
3月前
|
Web App开发 前端开发 iOS开发
CSS3 转换,深入理解Flutter动画原理,前端基础图形
CSS3 转换,深入理解Flutter动画原理,前端基础图形