0
0
Fluttermobile~20 mins

GestureDetector in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GestureDetector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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,
            ),
          ),
        ),
      ),
    );
  }
}
ANothing visible changes on screen, but 'Container tapped' is printed in the console.
BThe app crashes with a runtime error.
CThe blue container changes color to red when tapped.
DA popup dialog appears saying 'Container tapped'.
Attempts:
2 left
💡 Hint
Check what the onTap callback does in the GestureDetector.
lifecycle
intermediate
2: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)),
            ),
          ),
        ),
      ),
    );
  }
}
ANothing happens because onLongPress is not a valid GestureDetector callback.
BThe text inside the green box changes to 'Long pressed!' after holding for 2 seconds.
CThe app throws a setState error because message is not mutable.
DThe green box disappears from the screen.
Attempts:
2 left
💡 Hint
onLongPress triggers when the user presses and holds the widget.
navigation
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!')),
    );
  }
}
AThe app navigates to a new screen showing 'Welcome to the second screen!'.
BNothing happens because Navigator.of(context) cannot be used inside GestureDetector.
CThe app crashes with a runtime error about context.
DThe red container changes color to blue.
Attempts:
2 left
💡 Hint
Navigator.of(context).push opens a new screen on tap.
📝 Syntax
advanced
2: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,
  ),
)
AGestureDetector(onTap: () { print('Tapped'); }, child: Container(width: 100, height: 100, color: Colors.yellow))
B))wolley.sroloC :roloc ,001 :thgieh ,001 :htdiw(reniatnoC :dlihc ,} ;)'deppaT'(tnirp { )( :paTno(rotceteDerutseG
CGestureDetector(onTap: () => print('Tapped'), child: Container(width: 100, height: 100, color: Colors.yellow))
DGestureDetector(onTap: () { print('Tapped') child: Container(width: 100, height: 100, color: Colors.yellow))
Attempts:
2 left
💡 Hint
Look for missing punctuation or brackets.
🔧 Debug
expert
2: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,
  ),
)
ABecause GestureDetector only works with visible widgets that have text.
BBecause onTap requires the container to have a GestureRecognizer explicitly attached.
CBecause the container's color is transparent, it does not receive pointer events, so GestureDetector doesn't detect taps.
DBecause the onTap callback must be async to work properly.
Attempts:
2 left
💡 Hint
Try changing the container's color to a non-transparent color.