Sub topic 6
Detecting Taps, Double Taps and Long Presses with GestureDetector
This chapter assumes basic Dart closures, Flutter widget trees, StatefulWidget, setState, and layout with widgets such as Center, Padding, and Container.
GestureDetector is a widget that recognizes pointer patterns and calls your callbacks. It can detect a tap, double tap, long press, drag, scale, and other gestures. It does not provide visual feedback by itself. You must add that separately with a widget such as InkWell.
Tap-style callbacks
The most common callbacks are:
onTap: called after a complete tap.onDoubleTap: called when two taps happen close together.onLongPress: called when the pointer remains down long enough.onTapDown: called when contact starts.onTapUp: called when contact ends.onTapCancel: called when the tap attempt is interrupted.
The callback values are functions. Passing null disables that callback. The child determines the area that can receive the gesture.
import 'package:flutter/material.dart';
void main() {
runApp(const GestureExampleApp());
}
class GestureExampleApp extends StatelessWidget {
const GestureExampleApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: GesturePage(),
);
}
}
class GesturePage extends StatefulWidget {
const GesturePage({super.key});
@override
State<GesturePage> createState() => _GesturePageState();
}
class _GesturePageState extends State<GesturePage> {
String _message = 'Try a tap, double tap, or long press';
void _showMessage(String message) {
setState(() {
_message = message;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('GestureDetector')),
body: Center(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => _showMessage('Tap detected'),
onDoubleTap: () => _showMessage('Double tap detected'),
onLongPress: () => _showMessage('Long press detected'),
child: SizedBox(
width: 240,
height: 160,
child: Center(
child: Text(
_message,
textAlign: TextAlign.center,
),
),
),
),
),
);
}
}setState changes _message and schedules a rebuild. The Text widget then displays the new message.
behavior: HitTestBehavior.opaque makes the entire SizedBox receive pointer events, including its transparent parts. Without it, hit testing may only find areas painted by the child.
Callback timing and competing gestures
A tap is not recognized immediately. Flutter waits briefly to determine whether the pointer movement and timing match another gesture. For example, a double tap contains two tap attempts, so Flutter must distinguish it from two separate single taps. A long press also begins like a tap, but it wins only if the pointer remains down long enough.
Several recognizers can observe the same pointer sequence. Flutter uses a gesture arena to decide which recognizer wins. This means callbacks that seem independent can affect one another. Avoid assuming that onTap always runs before onDoubleTap; test the interaction you need.
Make the target usable
A gesture target should be large enough to locate and activate comfortably. Wrap a small visual icon in padding or a sized box instead of making only the icon respond. For an icon-only action, prefer IconButton when its built-in semantics and feedback fit your design.
Exercise
Exercise
Which property makes the complete 240 by 160 area in the example respond, even where the child does not paint a background?
Show answer
HitTestBehavior.opaque, assigned to the GestureDetector's behavior property.