DEV Community

Rui Kowase
Rui Kowase

Posted on

15 2 1 1 1

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.

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay