【发布时间】:2021-01-17 00:37:26
【问题描述】:
如果我从屏幕上滑出小部件,我有一个列表。在滑动时,我从列表中删除了第一个小部件,但我意识到该小部件的 dispose 方法没有被调用,因此 AnimationContoller 仍然由颤动持有并用于后续小部件。后续小部件的动画在它们被带到前面时结束。这使我的应用程序无法按预期运行。为了测试这一点,我在小部件的 initState 和 dispose 方法中打印了一个字符串。 initState 打印但 dispose 方法不打印。有什么帮助吗?
class ProfileWidget extends StatefulWidget {
ProfileWidget({Key key}) : super(key: key);
@override
State<StatefulWidget> createState() => _ProfileWidgetState();
}
class _ProfileWidgetState extends State<ProfileWidget> with TickerProviderStateMixin {
@override
void initState() {
super.initState();
print("New init");
_animationController = new AnimationController(
vsync: this,
duration: const Duration(milliseconds: 400),
);
_fadeAnimationController = new AnimationController(
vsync: this,
duration: const Duration(milliseconds: 200),
);
_fadeAnimation = new Tween(
begin: 0.0,
end: 1.0,
).animate(_fadeAnimationController);
_fadeAnimationController.forward();
}
@override
void dispose() {
print("Disposing");
if (_animationController != null) {
_animationController.dispose();
}
if (_fadeAnimationController != null) {
_fadeAnimationController.dispose();
}
super.dispose();
}
}
【问题讨论】:
标签: flutter