【发布时间】: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 并且你正在使用
SingleTickerProviderStateMixin。SingleTickerProviderStateMixin只能与一个动画控制器一起使用。将其替换为TickerProviderStateMixin,它应该可以工作。 -
@danypata 谢谢,你能把这个写成答案吗?
标签: flutter animation flutter-layout flutter-animation