What is the purpose of the GestureDetector widget in Flutter?
The GestureDetector widget detects gestures like taps, drags, and swipes on its child widget and lets you respond to them.
Click to reveal answer
beginner
Which property of GestureDetector handles a tap event?
The onTap property is a callback that runs when the user taps the widget.
Click to reveal answer
beginner
How do you make a widget respond to a double tap using GestureDetector?
Use the onDoubleTap property and provide a function to run when the user double taps the widget.
Click to reveal answer
intermediate
Can GestureDetector detect drag gestures? If yes, which callback is used?
Yes, it can detect drag gestures using callbacks like onPanUpdate which gives the drag movement details.
Click to reveal answer
intermediate
What happens if you wrap a button with GestureDetector and use onTap?
The GestureDetector will detect taps on the button and run your onTap code, allowing you to customize tap behavior.
Click to reveal answer
Which GestureDetector property detects a single tap?
AonTap
BonDoubleTap
ConLongPress
DonPanUpdate
✗ Incorrect
The onTap callback runs when the user taps once on the widget.
What type of widget must GestureDetector have inside it?
AA text widget only
BA parent widget
CNo widget needed
DA child widget to detect gestures on
✗ Incorrect
GestureDetector wraps a child widget to detect gestures on that widget.
Which callback detects when the user drags their finger across the screen?
AonTap
BonPanUpdate
ConDoubleTap
DonLongPress
✗ Incorrect
onPanUpdate detects drag movements and provides details about the drag.
If you want to detect a long press, which GestureDetector property do you use?
AonTap
BonDoubleTap
ConLongPress
DonPanEnd
✗ Incorrect
onLongPress runs when the user presses and holds on the widget.
What happens if you do not provide any gesture callbacks to GestureDetector?
AIt does nothing on gestures
BIt disables child widget
CIt throws an error
DIt still detects gestures automatically
✗ Incorrect
Without callbacks, GestureDetector does not respond to gestures.
Explain how to use GestureDetector to detect a tap and change the color of a box.
Think about wrapping a colored box and changing its color inside onTap.
You got /4 concepts.
Describe the difference between onTap and onDoubleTap in GestureDetector.
Consider how users interact with buttons or images differently.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of the GestureDetector widget in Flutter?
easy
A. To detect user touch gestures like taps and swipes on its child widget
B. To display images and icons on the screen
C. To manage app navigation between screens
D. To style text with different fonts and colors
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 A
Quick Check:
GestureDetector = Detects touch gestures [OK]
Hint: GestureDetector listens for user touches on child widgets [OK]
Common Mistakes:
Confusing GestureDetector with navigation widgets
Thinking it styles UI elements
Assuming it displays images
2. Which of the following is the correct way to detect a tap using GestureDetector in Flutter?
easy
A. GestureDetector(onPressed: () { print('Tapped'); }, child: Text('Tap me'))
B. GestureDetector(onClick: () { print('Tapped'); }, child: Text('Tap me'))
C. GestureDetector(onTouch: () { print('Tapped'); }, child: Text('Tap me'))
D. GestureDetector(onTap: () { print('Tapped'); }, child: Text('Tap me'))
Solution
Step 1: Identify the correct GestureDetector callback
The correct callback for detecting a tap is onTap. Other callbacks like onClick, onPressed, and onTouch do not exist on GestureDetector.
Step 2: Verify syntax correctness
GestureDetector(onTap: () { print('Tapped'); }, child: Text('Tap me')) uses the correct syntax with onTap and a child widget, making it valid Flutter code.
A. The text 'Tapped!' will be printed to the console
B. Nothing happens because onTap is not a valid callback
C. The app will crash due to missing onPressed callback
D. The text will change to 'Tapped!' on screen
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 A
Quick Check:
onTap prints 'Tapped!' on tap [OK]
Hint: onTap runs code on tap, prints to console if print() used [OK]
Common Mistakes:
Thinking onTap changes UI automatically
Confusing onTap with onPressed
Expecting visual changes without setState
4. Identify the error in this Flutter code snippet using GestureDetector:
B. onTap should be a function, not the result of print()
C. GestureDetector does not support onTap callback
D. Text widget cannot be a child of GestureDetector
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 B
Quick Check:
onTap needs a function, not immediate call [OK]
Hint: Pass a function to onTap, not a direct call [OK]
Common Mistakes:
Calling print() instead of passing a function
Omitting child widget
Using unsupported callbacks
5. You want to detect both tap and long press gestures on a button in Flutter. Which GestureDetector code correctly handles both events?
Step 1: Identify correct callbacks for tap and long press
GestureDetector uses onTap for taps and onLongPress for 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.