flutter - 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 上进行错误报告。
解决方案
这可能会在未来帮助其他用户:
SingleTickerProviderStateMixin
只有在有单个 AnimationController
.
如果有多个AnimationController
实例处于同一个状态,那么TickerProviderStateMixin
应该使用。
推荐阅读
- azure-ad-b2c - Azure AD B2C 电话 OTP 验证
- lua - LUA:#table 键引用突然改变表条目
- python - 在每个 X 循环中使用 Python 和 Pandas 保存到 excel
- php - 根据来自另一个数组的二进制值从一个数组中选择一个或多个值
- gitlab - 如何在 Gitlab 中为 Java 设置代码质量插件?
- python - 熊猫时间增量只有小时分钟和秒
- html - 引导 Vue 和 Axios
- javascript - 如何在没有消息对象 Discord.js 的情况下向特定频道发送消息
- python - 如何使用Django删除表中的一行
- android - 单击 Item Inside recycler 视图时,如何在 viewpager2 中膨胀它们各自的片段?