【问题标题】:Flutter, 2 animation controllers result in errorFlutter,2个动画控制器导致错误
【发布时间】:2021-04-09 13:45:26
【问题描述】:

我需要 2 个动画控制器,因为一个会永远播放,另一个只会播放一次。

但由于某种原因,在初始化第二个动画控制器时,应用程序崩溃了。

代码如下:

class _GlassTypeState extends State<GlassType>
    with SingleTickerProviderStateMixin {

  AnimationController _animationController;
  Animation _pulsateTween;

  AnimationController _opacityController;
  Animation _opacityTween;

  @override
  void initState() {
    super.initState();

    //WORKS Perfectly
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 1000),
    );
    _pulsateTween = ColorTween(begin: Colors.white24, end: Colors.white)
        .animate(_animationController);
    _animationController.repeat(reverse: true);     
    //----

    _opacityController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 1000),
    ); //ERROR happens here, if I comment this out no errors.

    // _opacityTween = Tween<double>(begin: 0, end: 1).animate(_opacityController);
    // _opacityController.forward();
  }

Flutter 的错误通常是没用的,但无论如何:

'package:flutter/src/widgets/framework.dart': Failed assertion: line 4345 pos 14: 'owner._debugCurrentBuildTarget == this': is not true.

type 'RenderErrorBox' is not a subtype of type 'RenderSemanticsGestureHandler' in type cast

关于更多上下文,我有一个在 build 方法中呈现的小部件 AnimatedBuilder() 小部件,这个小部件使用 _pulsateTween 补间并且工作正常。

我知道这一点,因为我尝试过渲染空 Container(),但在创建第二个 AnimationController 时它仍然崩溃。

PS:如果这对你有用,请通知我,我会在 github 上报告错误。

【问题讨论】:

  • 你有多个 AnimationControllers 并且你正在使用SingleTickerProviderStateMixinSingleTickerProviderStateMixin 只能与一个动画控制器一起使用。将其替换为TickerProviderStateMixin,它应该可以工作。
  • @danypata 谢谢,你能把这个写成答案吗?

标签: flutter animation flutter-layout flutter-animation


【解决方案1】:

这可能对未来的其他用户有所帮助:

SingleTickerProviderStateMixin 应仅在有单个AnimationController时使用。

如果有多个AnimationController实例处于同一状态,则应使用TickerProviderStateMixin

【讨论】:

    猜你喜欢
    • 2019-10-23
    • 2020-06-06
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2023-03-07
    • 2016-03-30
    • 2023-01-17
    • 1970-01-01
    相关资源
    最近更新 更多