在树构建逻辑中添加条件构造会导致挂载/卸载小部件。如果小部件内部带有控制器,这反过来又会导致恼人的动画问题。
您如何告诉Flutter阻止widget状态被释放,而是在树的另一部分中搜索它?
build(BuildContext context) {
var value = controller.value;
/// Let's avoid blinking at high opacity, save CPU at the same time
/// commenting this line will result in smooth animation
/// when it is uncommented, Flutter will dispose the State which was previously lying under Opacity() widget
if(value >= 0.95) return MyAnimatedWidget();
return Opacity(
opacity: value,
child: MyAnimatedWidget();
);
}发布于 2020-11-14 14:01:39
当您在代码中编写MyAnimatedWidget()时,这意味着您将再次启动一个新的窗口小部件实例,这就是为什么旧的窗口小部件被释放,因为它不再被使用。
您可以在类中创建一个MyAnimatedWidget()类型的字段,并将其与不透明或不透明一起返回。所以我将只是你的小部件的一个实例,我不会被处理掉。
MyAnimatedWidget animatedWidget = MyAnimatedWidget();
build(BuildContext context) {
var value = controller.value;
/// Let's avoid blinking at high opacity, save CPU at the same time
/// commenting this line will result in smooth animation
/// when it is uncommented, Flutter will dispose the State which was previously lying under Opacity() widget
if(value >= 0.95) return animatedWidget ;
return Opacity(
opacity: value,
child: animatedWidget ;
);
}https://stackoverflow.com/questions/64830948
复制相似问题