Gesture detection in Flutter TextSpan

GestureFlutter

Gesture Problem Overview


Is there a way to detect which word in the TextSpan was touched by the user?

This paragraph is here to get round the stack overflow robot that is insisting that I write more stuff :)

Gesture Solutions


Solution 1 - Gesture

You can improve by yourself

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'),
        )
      ]),
    );

Solution 2 - Gesture

Screenshot:

enter image description here


Use recognizer property of TextSpan which allows almost all types of event.

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.
        },
      ),
    ],
  ),
)

Solution 3 - Gesture

Iterate over the string to get an array of strings, create separate text span for each and add the gesture recognizer

 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;

Solution 4 - Gesture

To do the GestureRecognizer is their go-to "make something tappable". gesture recognizer that will receive events that hit this Span.

compose a simple widget containing a TextSpan with a recognizer.

Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          RichText(
            textAlign: TextAlign.center,
            text: TextSpan(children: <InlineSpan>[
              TextSpan(
                  text: 'By signing up, you agree to our ',
                  style: TextStyle(color: Colors.black87)),
              TextSpan(
                  recognizer: TapGestureRecognizer()..onTap = () {
                    print('Terms and Conditions Single Tap');
                  },
                  text: 'Terms and Conditions',
                  style: TextStyle(
                      color: Colors.blueAccent,
                      fontWeight: FontWeight.bold)),
            ]),
          ),
          RichText(
            textAlign: TextAlign.center,
            text: TextSpan(children: <InlineSpan>[
              TextSpan(
                  text: 'Already have an account ',
                  style: TextStyle(color: Colors.black87)),
              TextSpan(
                  recognizer: DoubleTapGestureRecognizer()..onDoubleTap = () {
                    print('Contact Us Double Tap');
                  },
                  text: 'Contact Us',
                  style: TextStyle(
                      color: Colors.blueAccent,
                      fontWeight: FontWeight.bold)),
            ]),
          )
        ],
      ))

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionadkoView Question on Stackoverflow
Solution 1 - GesturePutra ArdiansyahView Answer on Stackoverflow
Solution 2 - GestureCopsOnRoadView Answer on Stackoverflow
Solution 3 - GestureabubzView Answer on Stackoverflow
Solution 4 - GestureParesh MangukiyaView Answer on Stackoverflow