有没有办法识别触摸区域中的文本?

时间:2021-03-29 12:45:30

标签: flutter

我想识别触摸区域下方的文字,可以吗?下面我会提供一个样本

Text("This Is the Question 1")

我想确定用户是否点击了单词 {​​{1}},而不是整个文本

注意:RichText 不能满足我的要求,我无法将句子分成单词

1 个答案:

答案 0 :(得分:0)

试试这个

 GestureDetector(
            onTap: () {},
            child: Text("This Is the Question 1"),
          ),

如果您想让文本的特定部分可点击,请使用此代码。

          RichText(
            text: TextSpan(
              text: 'This Is the ',
              style: TextStyle(
                color: Colors.black,
                fontSize: 22.0,
              ),
              children: <TextSpan>[
                TextSpan(
                    text: 'Question',
                    style: TextStyle(
                      color: Colors.red,
                      fontSize: 22.0,
                    ),
                    recognizer: TapGestureRecognizer()
                      ..onTap = () {
                        print("It's Working");
                        // Do Your CLickable Logic Here
                      }),
                TextSpan(
                  text: ' 1',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 22.0,
                  ),
                ),
              ],
            ),
          ),