Flutter:我需要在StatelessWidget(TextSpan/recognizer)中调用GestureRecognizer.dispose吗?

3df52oht  于 2023-05-23  发布在  Flutter
关注(0)|答案(1)|浏览(115)

我正在阅读这篇文章:https://docs.flutter.io/flutter/painting/TextSpan/recognizer.html
此页面中包含的示例是StatefulWidget,文档显示为The code that owns the GestureRecognizer object must call GestureRecognizer.dispose when the TextSpan object is no longer used.
我想知道是否可以在StatelessWidget中使用TextSpanrecognizer
如果是,我需要在某个地方调用dispose吗?我不知道该怎么称呼它。

7vux5j2d

7vux5j2d1#

你不能在StatelessWidget中这样做。你必须将它转换为StatefulWidget,并覆盖Statedispose方法:

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();
  }
}

相关问题