Reputation: 39
I am trying to develop an app using Flutter that shows the user the translation of a single word. So if a user wants the translation of a word then he should double tap on that word and it shows him/her the translation.
Here I just don't want to get the String
value of a Text
Widget but I also want to get the word that the user double tapped on. So is there any way to do this?
Upvotes: 2
Views: 1352
Reputation: 9933
You can do it with RichText
. In my example, if you double tap any of highlighted words, it will be printed.
Widget build(BuildContext context) {
final testString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
final textSpans = <TextSpan>[];
testString.splitMapJoin(
RegExp('\\w+'),
onMatch: (m) {
final matchStr = m.group(0);
textSpans.add(TextSpan(
recognizer: DoubleTapGestureRecognizer()..onDoubleTap = () => print(matchStr),
text: matchStr,
style: TextStyle(background: Paint()..color = Colors.blue.withOpacity(0.4))
));
return matchStr;
},
onNonMatch: (string) {
textSpans.add(TextSpan(
text: string,
));
return string;
},
);
return RichText(
text: TextSpan(
style: TextStyle(fontSize: 30),
children: textSpans,
),
);
}
Upvotes: 1