使用SelectableText(Flutter)丢失了TextSpan的onTap回调

时间:2020-01-01 22:38:14

标签: flutter

我需要1)在某些文本(每章)中每个单词都可以接收点击(以实现单词突出显示),同时又不丢失2)本机文本选择(用于复制)。

我仅使用RichText实现了1),仅使用SelectableText实现了2),但从未实现。 SelectableText似乎没有传递事件。

每个单词必须是一个单独的TextSpan(与用于Text和SelectableText的注册识别器相同的跨度):

List<TextSpan> spans = [];
for(String word in chapterText) {
  spans.add(TextSpan(
    text:word,
    recognizer: new TapGestureRecognizer()..onTap = () => print('Tap from $word'),
  )
);

使用RichText

OnTap事件适用于每个TextSpan(因此识别器已正确设置)。

Container(
  child: RichText(text: TextSpan(children: spans)),
),

使用SelectableText

复制选择有效,但是每个TextSpan上的TapGestureRecognizer停止工作。

Container(
  child: SelectableText.rich(
    TextSpan(children: spans),
//  onTap: () => print("Common onTap event"),
  ),
),

我承认,本机选择工具已经在使用两次双击和长按事件(至少在iOS上)。 SelectableText.rich有其自己的onTap事件(已注释),该事件可能不会将该事件传递给基础TextSpans。

在文档GestureRecognizer中仅讨论RichText,而不是SelectableText(稍后添加)。

这是预期的行为,并且有解决方法吗? 任何提示表示赞赏。

PS。使用Flutter(在Mac OS X 10.14.6上,通道稳定,v1.12.13 + hotfix.5)和Xcode 11.0和iOS Simulator 11.0。

0 个答案:

没有答案