Explain it simply
Handling Gestures, in plain language
If you want user interaction on widgets that aren't buttons — like Container, Image, Card, or Icon — you can wrap them with GestureDetector. It's commonly used for things like tapping a product card to go to its detail page, or double-tapping an image to mark it as a favorite.
Problem → Why → Solution
The problem — why this concept matters
If you put a GestureDetector without a child, there's no area to tap. Make sure to include a child widget.
A common mistake
Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.
Core concept — understand how it works
For Handling Gestures, define the source of truth, validation, focus, controller disposal, and minimal rebuild boundary. Keep ephemeral state local and place shared app state in a testable controller or model.
- input and state
- widget build and layout
- rendered Flutter UI
Explain the widget, state, and lifecycle behavior of Handling Gestures
Core syntax or pattern
GestureDetector(
onTap: () {
print('Card tapped');
},
onLongPress: () {
print('Card long pressed');
},
child: Container(
width: 180,
padding: const EdgeInsets.all(18),
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(14),
),
child: const Center(
child: Text('Tap this card'),
),
),
)For Handling Gestures, define the source of truth, validation, focus, controller disposal, and minimal rebuild boundary. Keep ephemeral state local and place shared app state in a testable controller or model.
Read the code step by step
- input and state
GestureDetector(If you want user interaction on widgets that aren't buttons — like Container, Image, Card, or Icon — you can wrap them with GestureDetector. It's commonly used for things like tapping a product card to go to its detail page, or double-tapping an image to mark it as a favorite.
- widget build and layout
padding: const EdgeInsets.all(18),For Handling Gestures, define the source of truth, validation, focus, controller disposal, and minimal rebuild boundary. Keep ephemeral state local and place shared app state in a testable controller or model.
- rendered Flutter UI
)Run and test an original Handling Gestures example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
- input and state
- widget build and layout
- rendered Flutter UI
Success
Explain the widget, state, and lifecycle behavior of Handling Gestures
Failure
If you put a GestureDetector without a child, there's no area to tap. Make sure to include a child widget.
If you want user interaction on widgets that aren't buttons — like Container, Image, Card, or Icon — you can wrap them with GestureDetector. It's commonly used for things like tapping a product card to go to its detail page, or double-tapping an image to mark it as a favorite.
GestureDetector(
onTap: () {
print('Card tapped');
},
onLongPress: () {
print('Card long pressed');
},
child: Container(
width: 180,
padding: const EdgeInsets.all(18),
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(14),
),
child: const Center(
child: Text('Tap this card'),
),
),
)A green card appears, and tapping or long-pressing it prints a message to the console.Next Steps
Next, learn about TextField and form input, where users can type in text.
Real-world usage
How to apply it in a real project
Run and test an original Handling Gestures example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
Mini challenge
Build it yourself
Run and test an original Handling Gestures example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
GestureDetector(
onTap: () {
print('Card tapped');
},
onLongPress: () {
print('Card long pressed');
},
child: Container(
width: 180,
padding: const EdgeInsets.all(18),
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(14),
),
child: const Center(
child: Text('Tap this card'),
),
),
)Expected: Explain the widget, state, and lifecycle behavior of Handling Gestures
Show hint
Run and test an original Handling Gestures example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- input and state → widget build and layout → rendered Flutter UI
- For Handling Gestures, define the source of truth, validation, focus, controller disposal, and minimal rebuild boundary. Keep ephemeral state local and place shared app state in a testable controller or model.
- If you put a GestureDetector without a child, there's no area to tap. Make sure to include a child widget.
- Run and test an original Handling Gestures example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
Flutter documentation — Flutter