Created
April 13, 2023 00:19
-
-
Save BarryDaBee/822cccad99801331a5b1438d36f1197f to your computer and use it in GitHub Desktop.
Sample showing how to create Text with clickable words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/gestures.dart'; | |
import 'package:flutter/material.dart'; | |
class ClickableTextView extends StatelessWidget { | |
final String text; | |
const ClickableTextView({Key? key, required this.text}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return RichText( | |
text: TextSpan( | |
style: const TextStyle( | |
color: Colors.black, | |
fontSize: 24, | |
), | |
children: text | |
.split(' ') | |
.map( | |
(word) => TextSpan( | |
text: '$word ', | |
recognizer: LongPressGestureRecognizer() | |
..onLongPress = () { | |
showDialog( | |
context: context, | |
builder: (context) { | |
return Dialog( | |
backgroundColor: Colors.white, | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
Text( | |
word.replaceAll(RegExp('[^a-zA-Z]'), ''), | |
style: | |
Theme.of(context).textTheme.headlineLarge, | |
) | |
], | |
), | |
); | |
}, | |
); | |
}, | |
), | |
) | |
.toList(), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment