All 7 Flutter Gestures & Touch Handling questions
More in Flutter Gestures & Touch Handling
Advertisement
Flutter › Flutter Gestures & Touch Handling

What is the GestureDetector widget in Flutter, and how does it let you detect common touch interactions, such as a tap, a double tap, or a long press, on any given widget?

Beginner

Answer

GestureDetector is a genuinely versatile widget that wraps around any other child widget and listens for a wide variety of common touch interactions, including a simple tap, a double tap, a long press, and various different drag gestures, letting you attach specific callback functions that automatically get called the moment each particular corresponding gesture is actually detected, and this makes it possible to add meaningful interactivity to essentially any visual widget, including ones like a plain Container or an Image that do not otherwise have any built in interactive tap behavior of their own.
GestureDetector(
  onTap: () => print('Tapped!'),
  onLongPress: () => print('Long pressed!'),
  child: Container(color: Colors.blue, width: 100, height: 100),
)
Real-world example A photo gallery app wraps each individual thumbnail image with a GestureDetector, opening that specific photo in full screen view on a regular single tap, while showing a context menu with additional sharing and deletion options specifically on a long press instead.

Common follow-ups: What is the difference between GestureDetector and the simpler InkWell widget?;Can a single GestureDetector actually detect several different gesture types simultaneously on exactly the same widget?