问题描述
我正在阅读此文档页面: https://docs.flutter.io/flutter/painting/TextSpan/recognizer.html .
I am reading this doc page: https://docs.flutter.io/flutter/painting/TextSpan/recognizer.html.
此页面中包含的示例是一个StatefulWidget,文档说拥有GestureRecognizer对象的代码必须调用GestureRecognizer.dispose,当不再使用TextSpan对象时.
.
The example included in this page is a StatefulWidget and the doc says The code that owns the GestureRecognizer object must call GestureRecognizer.dispose when the TextSpan object is no longer used.
.
我想知道是否可以在StatelessWidget中使用 TextSpan
的 recognizer
?
I am wondering if I can use the recognizer
of TextSpan
in a StatelessWidget??
如果是,我是否需要在某个地方打电话给 dispose
?我不知道该在哪里打电话.
If so, do I need to call dispose
somewhere? I have no idea where to call it.
非常感谢.
推荐答案
您不能在 StatelessWidget
中执行此操作.您将不得不将其转换为 StatefulWidget
,并覆盖 State
的 dispose
方法:
You cannot do that in a StatelessWidget
. You will have to convert it into a StatefulWidget
and override the dispose
method of State
:
class Foo extends StatefulWidget {
@override
_FooState createState() => _FooState();
}
class _FooState extends State<Foo> {
GestureRecognizer gestureRecognizer;
@override
void dispose() {
gestureRecognizer?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container();
}
}
这篇关于Flutter:我需要在StatelessWidget(TextSpan/recognizer)中调用GestureRecognizer.dispose吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!