flutter - 如何向 TextSpan 添加多个手势识别器?
问题描述
我想将 TapGestureRecognizer 和 LongPressGestureRecognizer 添加到TextSpan。现在我可以添加任何一个,但不能同时添加。
我查看了 GestureDetector 类并想用它包装 TextSpan,但 RichText 元素只接受 TextSpan 而不是小部件。
这就是我现在所拥有的:
TextSpan(
text: "some text",
recognizer: TapGestureRecognizer()
..onTap = () { print('tapped'); }
)
我想..onLongPress
在该代码中添加某处。
最后,两个手势都应该在单个文本跨度上工作。
解决方案
您可以使用WidgetSpan设置您的跨度并通过 GestureDetector 检测 TapGestureRecognizer 和 LongPressGestureRecognizer
TextSpan(
children: <InlineSpan>[
TextSpan(text: 'Flutter is'),
WidgetSpan(
child: GestureDetector(
onTap: () {
},
onLongPress: () {
},
child: Text(' Hello World! '),
)
),
TextSpan(text: 'the best!'),
],
)
推荐阅读
- mqtt - 从 Ablem 到 MQTT 的通信
- c - 当 ftruncate 到 0 第一个磁盘块释放?
- algorithm - 直线上的最大点数 O(N)
- asp.net-core - aspnet core 获取配置的路由模板
- python - 如何在 Python 中同时遍历两个列表的元素
- python - 通过字符串迭代变量
- excel - 删除 Excel 个别格式条件
- postgresql - 与 Postgresql 11 合并
- react-native - 当整个应用程序在 Windows 上编写时,在 Xcode 上 React Native IPA Build
- chart.js - 如何在同一侧chartjs React对齐标签