防止 ValueListenableBuilder 或 AnimatedBuilder 在 valueListenable 未更改时重建

时间:2021-04-27 16:09:42

标签: flutter animation

我目前正在 Flutter 中构建一个复杂的测验动画。 它“按预期”工作,但我发现了一些性能问题。 我的动画是 6000 毫秒长。 我有一个带有 3 个 TweenSequenceTweenSequenceItems。 开始时为 5%,中间为 90%(恒定值),最后为 5%。 这是序列的代码:

  QuizAnimation({this.controller})
      : translateQuestion = 
        TweenSequence(<TweenSequenceItem<double>>[
          TweenSequenceItem<double>(
            tween: Tween<double>(begin: 1.0, end: 0.0)
                .chain(CurveTween(curve: Curves.easeOutExpo)),
            weight: 5.0,
          ),
          TweenSequenceItem<double>(
            tween: ConstantTween<double>(0.0),
            weight: 90.0,
          ),
          TweenSequenceItem<double>(
            tween: Tween<double>(begin: 0.0, end: -1.0)
                .chain(CurveTween(curve: Curves.easeInExpo)),
            weight: 5.0,
          ),
        ]).animate(
          CurvedAnimation(
            parent: controller,
            curve: Interval(0.0, 1.0, curve: Curves.linear),
          ),
        ),
     questionNumberOpacity = TweenSequence(<TweenSequenceItem<double>>[...],
     (...)

这是我的 AnimationController 初始化:

  void initAnimation() {
    animController = AnimationController(
        vsync: this, duration: Duration(milliseconds: 6000));
    animation = QuizAnimation(controller: animController);
    animController.animateTo(0.5);
  }

现在是构建 Widget 的部分:

ValueListenableBuilder(
   valueListenable: animation.translateQuestion,
   builder: (context, value, child) {
     print(animation.translateQuestion.toString());
     return Transform.translate(
               offset: Offset(value * 500, 0),
               child: ComplexWidget(),
);
    )
}

现在,当动画开始时,值当然会从 1.0 变为 0.0,如 TweenSequence 中给出的那样。

AnimationController#ec925(▶ 0.000)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩1.0
AnimationController#ec925(▶ 0.003)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.6972004771232605
AnimationController#ec925(▶ 0.009)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.28010737150907516
AnimationController#ec925(▶ 0.015)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.10956916958093643
AnimationController#ec925(▶ 0.018)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.06999024003744125
AnimationController#ec925(▶ 0.021)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.0441800132393837
AnimationController#ec925(▶ 0.024)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.02774503082036972
AnimationController#ec925(▶ 0.030)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.010052566416561604
AnimationController#ec925(▶ 0.032)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.005614541471004486
AnimationController#ec925(▶ 0.038)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.00134380254894495
AnimationController#ec925(▶ 0.041)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.0005135014653205872
AnimationController#ec925(▶ 0.044)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.0001309514045715332
AnimationController#ec925(▶ 0.050)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.0 
AnimationController#ec925(▶ 0.053)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.0 // i do not want these, prevent rebuild
AnimationController#ec925(▶ 0.059)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.0 // i do not want these, prevent rebuild
AnimationController#ec925(▶ 0.065)➩Interval(0.0⋯1.0)➩TweenSequence(3 items)➩0.0 // i do not want these, prevent rebuild

我的问题是:如果值为 ValueListenableBuilder / constant,我如何防止在 ConstantTween 内重建?我需要创建多个 AnimationController 来实现吗?我真的很喜欢使用我的 StaggeredAnimation 来更灵活地对值的变化做出反应。也许我在这里遗漏了一些东西。 PS:我试过 AnimatedBuilder - 但它产生了相同的结果。

期待您的回答。

0 个答案:

没有答案