DEV Community

Rui Kowase
Rui Kowase

Posted on

How to add a click event to any widget of Flutter

How to add a click event to any widget of Flutter.

TL;DR

Use GestureDetector widget

Sample code

GestureDetector(
  onTap: () {
    print("onTap called.");
  },
  child: Text("foo"),
),
Enter fullscreen mode Exit fullscreen mode

Environment

  • Flutter 0.1.5
  • Dart 2.0.0-dev.28.0

Link

user interface - Flutter - Container onPressed? - Stack Overflow
https://stackoverflow.com/questions/43692923/flutter-container-onpressed

Top comments (3)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
tayormi profile image
Temitope Ajiboye

Have you tried using InkWell()?

Collapse
 
mxglt profile image
Maxime Guilbert

On the onTap behaviour it's the same
But there is a huge gap as an InkWell will have a "hover" style, changing the background color and will have a click effect. If you don't want to have this, the GestureDetector is what you need.