Gesture detection in Flutter TextSpan

Solution 1:

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:

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:

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:

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