Challenge - 5 Problems
GestureDetector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Tap Gesture Detection Output
What will be the output when the user taps the blue container in this Flutter app?
Flutter
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( body: Center( child: GestureDetector( onTap: () { print('Container tapped'); }, child: Container( width: 100, height: 100, color: Colors.blue, ), ), ), ), ); } }
Attempts:
2 left
💡 Hint
Check what the onTap callback does in the GestureDetector.
✗ Incorrect
The GestureDetector listens for taps and runs the onTap callback. Here, it prints a message to the console but does not change the UI.
❓ lifecycle
intermediate2:00remaining
GestureDetector onLongPress Behavior
What happens when the user presses and holds the green container for 2 seconds in this Flutter app?
Flutter
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatefulWidget { const MyApp({super.key}); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { String message = 'Hold the box'; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: GestureDetector( onLongPress: () { setState(() { message = 'Long pressed!'; }); }, child: Container( width: 150, height: 150, color: Colors.green, alignment: Alignment.center, child: Text(message, style: const TextStyle(color: Colors.white)), ), ), ), ), ); } }
Attempts:
2 left
💡 Hint
onLongPress triggers when the user presses and holds the widget.
✗ Incorrect
The onLongPress callback updates the state variable message, which rebuilds the widget showing new text.
advanced
2:00remaining
GestureDetector and Navigation Behavior
What happens when the user taps the red container in this Flutter app?
Flutter
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( body: Center( child: GestureDetector( onTap: () { Navigator.of(context).push(MaterialPageRoute( builder: (context) => const SecondScreen(), )); }, child: Container( width: 120, height: 120, color: Colors.red, alignment: Alignment.center, child: const Text('Tap me', style: TextStyle(color: Colors.white)), ), ), ), ), ); } } class SecondScreen extends StatelessWidget { const SecondScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Second Screen')), body: const Center(child: Text('Welcome to the second screen!')), ); } }
Attempts:
2 left
💡 Hint
Navigator.of(context).push opens a new screen on tap.
✗ Incorrect
The onTap callback uses Navigator to push a new route, showing the second screen with the welcome message.
📝 Syntax
advanced2:00remaining
GestureDetector Syntax Error Identification
Which option contains a syntax error that will prevent the Flutter app from compiling?
Flutter
GestureDetector(
onTap: () {
print('Tapped');
},
child: Container(
width: 100,
height: 100,
color: Colors.yellow,
),
)Attempts:
2 left
💡 Hint
Look for missing punctuation or brackets.
✗ Incorrect
Option D is missing a comma between the onTap callback and the child property, causing a syntax error.
🔧 Debug
expert2:00remaining
Why does GestureDetector not detect taps?
Given this Flutter code, why does tapping the container not trigger the onTap callback?
Flutter
GestureDetector(
onTap: () {
print('Tapped');
},
child: Container(
width: 100,
height: 100,
color: Colors.transparent,
),
)Attempts:
2 left
💡 Hint
Try changing the container's color to a non-transparent color.
✗ Incorrect
A transparent container does not block pointer events, so GestureDetector cannot detect taps on it. Giving it a color with opacity > 0 fixes this.