Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gesture detection in Flutter TextSpan

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 :)

like image 766
adko Avatar asked Feb 21 '18 20:02

adko


People also ask

What is TextSpan Flutter?

TextSpan is an immutable span of text. It has style property to give style to the text. It is also having children property to add more text to this widget and give style to the children.

How do you center a rich text Flutter?

Flutter – Center Align Text in Text Widget To center align the text in a Text widget, provide textAlign property with value TextAlign. center .


2 Answers

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'),         )       ]),     ); 
like image 116
Putra Ardiansyah Avatar answered Sep 23 '22 02:09

Putra Ardiansyah


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.         },       ),     ],   ), )  
like image 30
CopsOnRoad Avatar answered Sep 19 '22 02:09

CopsOnRoad