首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flutter TextSpan中的手势检测

Flutter TextSpan中的手势检测
EN

Stack Overflow用户
提问于 2018-02-22 04:20:14
回答 4查看 38.9K关注 0票数 69

有没有办法检测用户触摸了TextSpan中的哪个单词?

这段话是为了绕过堆栈溢出机器人,它坚持让我写更多的东西:)

EN

回答 4

Stack Overflow用户

发布于 2018-04-25 05:47:29

你可以自己提高自己

代码语言:javascript
复制
import 'package:flutter/gestures.dart';
...

new RichText(
      text: new TextSpan(text: 'Non touchable. ', children: [
        new TextSpan(
          text: 'Tap here.',
          recognizer: new TapGestureRecognizer()..onTap = () => print('Tap Here onTap'),
        )
      ]),
    );
票数 120
EN

Stack Overflow用户

发布于 2019-04-10 15:40:55

截图:

使用TextSpanrecognizer属性,它允许几乎所有类型的事件。

代码语言:javascript
复制
RichText(
  text: TextSpan(
    children: [
      TextSpan(
        text: 'Single tap',
        style: TextStyle(color: Colors.red[300]),
        recognizer: TapGestureRecognizer()..onTap = () {
          // Single tapped.
        },
      ),
      TextSpan(
        text: ' Double tap',
        style: TextStyle(color: Colors.green[300]),
        recognizer:  DoubleTapGestureRecognizer()..onDoubleTap = () {
          // Double tapped.
        }
      ),
      TextSpan(
        text: ' Long press',
        style: TextStyle(color: Colors.blue[300]),
        recognizer: LongPressGestureRecognizer()..onLongPress = () {
          // Long Pressed.
        },
      ),
    ],
  ),
)
票数 53
EN

Stack Overflow用户

发布于 2020-01-08 17:25:03

迭代字符串以获得字符串数组,为每个字符串创建单独的文本范围,并添加手势识别器

代码语言:javascript
复制
 List<TextSpan> createTextSpans(){
    final string = """Text seems like it should be so simple, but it really isn't.""";
    final arrayStrings = string.split(" ");
    List<TextSpan> arrayOfTextSpan = [];
    for (int index = 0; index < arrayStrings.length; index++){
      final text = arrayStrings[index] + " ";
      final span = TextSpan(
        text: text,
        recognizer: TapGestureRecognizer()..onTap = () => print("The word touched is $text")
      );
      arrayOfTextSpan.add(span);
    }
    return arrayOfTextSpan;
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48914775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档