GestureDetector lets your app notice when you touch or swipe the screen. It helps make your app interactive.
GestureDetector in Flutter
GestureDetector(
onTap: () {
// code to run when tapped
},
child: Widget(),
)The child is the widget that listens for gestures.
You can use many gesture callbacks like onTap, onLongPress, onPanUpdate, etc.
GestureDetector(
onTap: () {
print('Tapped!');
},
child: Container(color: Colors.blue, width: 100, height: 100),
)GestureDetector(
onLongPress: () {
print('Long pressed!');
},
child: Text('Hold me'),
)GestureDetector(
onPanUpdate: (details) {
print('Dragged by ${details.delta}');
},
child: Icon(Icons.drag_handle),
)This app shows a teal box with the text "Tap me". When you tap the box, a message appears at the bottom saying "You tapped the box!".
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('GestureDetector Example')), body: Center( child: GestureDetector( onTap: () { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('You tapped the box!')), ); }, child: Container( width: 150, height: 150, color: Colors.teal, alignment: Alignment.center, child: const Text( 'Tap me', style: TextStyle(color: Colors.white, fontSize: 24), ), ), ), ), ), ); } }
GestureDetector does not show any visual change by itself. You can add visual feedback inside the gesture callbacks.
Make sure the child widget has a size; otherwise, gestures might not be detected.
Use HitTestBehavior if you want to control how gestures pass through overlapping widgets.
GestureDetector listens for touch gestures on its child widget.
Use it to detect taps, long presses, drags, and more.
It helps make your app interactive and responsive to user touches.
Practice
GestureDetector widget in Flutter?Solution
Step 1: Understand GestureDetector's role
The GestureDetector widget listens for user touch gestures such as taps, double taps, long presses, and drags on its child widget.Step 2: Compare with other options
Displaying images and icons, managing app navigation, and styling text are handled by other widgets like Image/Icon, Navigator/Router, and TextStyle, not GestureDetector.Final Answer:
To detect user touch gestures like taps and swipes on its child widget -> Option AQuick Check:
GestureDetector = Detects touch gestures [OK]
- Confusing GestureDetector with navigation widgets
- Thinking it styles UI elements
- Assuming it displays images
Solution
Step 1: Identify the correct GestureDetector callback
The correct callback for detecting a tap isonTap. Other callbacks likeonClick,onPressed, andonTouchdo not exist on GestureDetector.Step 2: Verify syntax correctness
GestureDetector(onTap: () { print('Tapped'); }, child: Text('Tap me')) uses the correct syntax withonTapand a child widget, making it valid Flutter code.Final Answer:
GestureDetector(onTap: () { print('Tapped'); }, child: Text('Tap me')) -> Option DQuick Check:
Tap detection uses onTap callback [OK]
- Using onClick or onPressed instead of onTap
- Forgetting to provide a child widget
- Misspelling callback names
GestureDetector(
onTap: () => print('Tapped!'),
child: Text('Click me'),
)Solution
Step 1: Understand onTap behavior
The onTap callback runs the provided function when the user taps the child widget. Here, it prints 'Tapped!' to the console.Step 2: Check other options
The text does not change on screen automatically without state management like setState. onTap is valid, so something does happen on tap. The app won't crash because onPressed is not required for GestureDetector.Final Answer:
The text 'Tapped!' will be printed to the console -> Option AQuick Check:
onTap prints 'Tapped!' on tap [OK]
- Thinking onTap changes UI automatically
- Confusing onTap with onPressed
- Expecting visual changes without setState
GestureDetector(
onTap: print('Tapped'),
child: Text('Tap me'),
)Solution
Step 1: Analyze onTap assignment
The onTap callback expects a function to call when tapped. Here,print('Tapped')runs immediately and assigns its result (void) to onTap, which is incorrect.Step 2: Correct usage
onTap should be assigned a function like() => print('Tapped')or(){ print('Tapped'); }to run on tap.Final Answer:
onTap should be a function, not the result of print() -> Option BQuick Check:
onTap needs a function, not immediate call [OK]
- Calling print() instead of passing a function
- Omitting child widget
- Using unsupported callbacks
Solution
Step 1: Identify correct callbacks for tap and long press
GestureDetector usesonTapfor taps andonLongPressfor long presses. GestureDetector(onTap: () => print('Tap'), onLongPress: () => print('Long press'), child: Container()) uses both correctly.Step 2: Check other options for errors
GestureDetector(onTap: () => print('Tap'), onDoubleTap: () => print('Long press'), child: Container()) uses onDoubleTap instead of onLongPress, which is incorrect. GestureDetector(onPressed: () => print('Tap'), onLongPress: () => print('Long press'), child: Container()) uses onPressed which GestureDetector does not have. GestureDetector(onTap: () => print('Tap'), onHold: () => print('Long press'), child: Container()) uses onHold which is not a valid callback.Final Answer:
GestureDetector(onTap: () => print('Tap'), onLongPress: () => print('Long press'), child: Container()) -> Option CQuick Check:
Use onTap and onLongPress for tap and long press [OK]
- Using onPressed instead of onTap
- Confusing onDoubleTap with onLongPress
- Using non-existent callbacks like onHold
Ready to prove it?
Pass the test and this pattern moves unknown
