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!
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.
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"), )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With