首页 > 解决方案 > Flutter,2个动画控制器导致错误

问题描述

我需要有 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

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

我知道这一点,因为我尝试渲染 empty Container(),但在创建 2nd 时它仍然崩溃AnimationController

PS:如果这对您有用,请通知我,我将在 github 上进行错误报告。

标签: flutteranimationflutter-layoutflutter-animation

解决方案


这可能会在未来帮助其他用户:

SingleTickerProviderStateMixin只有在有单个 AnimationController.

如果有多个AnimationController实例处于同一个状态,那么TickerProviderStateMixin应该使用。


推荐阅读