Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make some text tappable (respond to taps) in Flutter?

Tags:

Assume I have some Flutter code like this:

  // ...   body: new Center(     child: new Text(       'Hello, I am some text',     ),   ),   // ... 

How can I make the Text on the screen respond to a tap? (For example, simply printing to the log when I tap the text.)

Thanks!

like image 849
Seth Ladd Avatar asked Jun 09 '17 21:06

Seth Ladd


People also ask

How do I use GestureDetector on text in Flutter?

In BuildContext() we return Scaffold() and use the property of AppBar() and pass the title of the application. In the body of Scaffold(), we pass Center() and after that, we pass GestureDetector() as a child of it. We create a Container() and pass the color and text which is already passed the code.


1 Answers

As seen on this answer, you can use an InkWell or a gesture detector.

For example

InkWell(     child: Text("Hello"),     onTap: () {print("value of your text");}, ) 

Or

var textValue = "Flutter" InkWell(     child: Text(textValue),     onTap: () {print(textValue);}, ) 

EDIT : As Collin Jackson suggested, you can also use a FlatButton

FlatButton(   onPressed: () {print("Hello world");},   child: Text("Hello world"), ); 

If you don't need or require material (FlatButton, InkWell, etc), you can use GestureDetector:

GestureDetector(   onTap: () { print("I was tapped!"); },   child: Text("Hello world"), ) 
like image 88
Alexi Coard Avatar answered Oct 15 '22 12:10

Alexi Coard