Sub topic 11
How competing gestures are resolved in the gesture arena
This chapter assumes you can build widget trees with StatefulWidget and setState. It also assumes you have used GestureDetector for taps, long presses and pan callbacks, and that you know that widgets can be nested in Stack, Row and Column. Here you learn why only one of several overlapping gesture callbacks fires, and how to change the outcome when the default is wrong.
The problem: one touch, many listeners
A single finger touch can mean different things. A press on a list row could become a tap, a long press, a vertical scroll or a horizontal swipe, and the app cannot tell which until the finger has moved or lifted. Several widgets in the tree may also want the same touch, such as a button inside a scrolling list inside a swipeable page.
Flutter's answer is the gesture arena. It is a small contest, created for each pointer (finger), in which gesture recognizers compete. Exactly one recognizer wins, and the others are told they lost and drop the pointer.
How the arena works
A gesture recognizer is an object that watches raw pointer events and decides whether they form a specific gesture. GestureDetector creates recognizers for you based on the callbacks you pass. For example, onTap creates a TapGestureRecognizer, and onPanUpdate creates a PanGestureRecognizer.
The sequence for one pointer is:
- The finger touches down. Flutter runs a hit test (see the hit testing chapter) and builds a list of render objects under the finger, from the deepest child up to the root.
- Each hit target that has a recognizer adds the recognizer to the arena for that pointer. The deepest widget's recognizers are added first.
- Raw pointer events (move, up, cancel) are delivered to every recognizer in the arena. Each recognizer watches and decides whether the events match its gesture.
- A recognizer can accept (claim victory) or reject (declare defeat). When one accepts, every other recognizer is rejected at once.
- If the pointer lifts and no recognizer has accepted, the arena is swept. The first recognizer still in the arena wins by default.
- If only one recognizer remains because all others rejected, that recognizer wins immediately.
Each recognizer has its own rule for when to accept or reject:
| Recognizer | Accepts when | Rejects when |
|---|---|---|
| Tap | Pointer lifts (via the sweep or a direct claim) | Pointer moves past the touch slop, or a long press wins |
| Long press | Pointer stays down for about 500 ms | Pointer moves past the slop first |
| Vertical or horizontal drag | Movement along its axis passes the slop | Movement is mainly along the other axis, or pointer lifts first |
| Pan | Movement in any direction passes the pan slop | Pointer lifts first |
| Double tap | A second tap arrives within about 300 ms of the first | The timeout expires, or the pointer moves too far |
The slop is a small distance a finger may wander before Flutter treats the touch as a movement. It exists because a real finger never holds perfectly still. A tap needs the pointer to stay within roughly 18 logical pixels (kTouchSlop), and a pan uses a larger value (kPanSlop).
Nested taps: the child wins
Here is a complete app with two nested tap detectors.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: ArenaDemo())));
class ArenaDemo extends StatelessWidget {
const ArenaDemo({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: GestureDetector(
onTap: () => debugPrint('outer tap'),
child: Container(
width: 200,
height: 200,
color: Colors.amber,
alignment: Alignment.center,
child: GestureDetector(
onTap: () => debugPrint('inner tap'),
child: Container(width: 100, height: 100, color: Colors.indigo),
),
),
),
);
}
}Tap the blue square and the console prints only inner tap. Both detectors added a TapGestureRecognizer, the inner one first, because hit testing goes from the deepest widget outward. On pointer up nobody had claimed victory, so the sweep gave the win to the first member, the inner one. Tap the amber area outside the square and only outer tap prints, because the inner detector was never hit.
This is why a button inside a tappable card does not also trigger the card. It is also why you cannot make both callbacks fire by nesting two onTap handlers. If you need both, call the parent's logic from the child, or observe raw events with Listener (see below).
Delay costs: tap versus double tap
When one detector has both onTap and onDoubleTap, the tap recognizer cannot fire on the first pointer up. A second tap might still arrive, and that would make it a double tap. The double tap recognizer holds the arena open for the double-tap timeout. Single taps therefore feel sluggish by about 300 ms.
GestureDetector(
onTap: () => debugPrint('single'),
onDoubleTap: () => debugPrint('double'),
child: const SizedBox(width: 120, height: 120, child: Placeholder()),
)If you do not need double taps, leave onDoubleTap out and taps become instant. This delay is a design cost of the arena, not a bug. A recognizer cannot know the future, so it has to wait.
Drag versus drag: axes decide
A horizontal drag detector inside a vertical ListView is a common pattern. Both drag recognizers join the arena on pointer down. Neither wins until the finger moves. Once movement passes the slop, the recognizer whose axis matches the dominant direction accepts, and the other is rejected. A mostly horizontal swipe goes to your detector, and a mostly vertical one scrolls the list.
Diagonal movement is the hard case. If you use onPanUpdate (any direction) inside a scrollable, the pan recognizer and the scroll recognizer compete, and the one whose slop is passed first usually wins. This is why a pan-based widget in a list may feel like it fights the scroll. Prefer an axis-specific callback (onHorizontalDragUpdate) when only one axis matters.
Changing the outcome with RawGestureDetector
GestureDetector is a convenience wrapper. RawGestureDetector lets you choose the recognizer directly. A common use is claiming every touch immediately so that a parent scroll view cannot steal it, for example over an embedded map or a canvas.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
class ClaimsEveryTouch extends StatelessWidget {
const ClaimsEveryTouch({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
return RawGestureDetector(
gestures: {
EagerGestureRecognizer:
GestureRecognizerFactoryWithHandlers<EagerGestureRecognizer>(
() => EagerGestureRecognizer(),
(EagerGestureRecognizer instance) {},
),
},
child: child,
);
}
}EagerGestureRecognizer accepts the pointer as soon as it goes down, so it wins before any scroll recognizer can. Place a widget that handles its own drags inside it.
Listener sits outside the arena
Listener reports raw pointer events (onPointerDown, onPointerMove, onPointerUp) and never joins the arena. Its callbacks fire for every event that reaches it, even when a gesture recognizer elsewhere has won. Use it for logging, hover tracking or custom hit logic. Do not use it to build taps, because it cannot tell a tap from the start of a scroll.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Parent `onTap` never fires when the child also has `onTap` | The child's recognizer joined the arena first and won the sweep | Move the logic into the child, or make the child non-tappable |
| Taps feel about 300 ms late | `onDoubleTap` is set on the same detector | Remove `onDoubleTap`, or accept the delay |
| Drag stops the list from scrolling | The pan or eager recognizer wins before the scroll starts | Use an axis-specific drag callback |
| Widget in a scroll view cannot be dragged | The scroll recognizer wins on that axis | Use `RawGestureDetector` with a recognizer that wins sooner |
| Callbacks fire for gestures another widget handled | You used `Listener` | Use `GestureDetector` if you need arena rules |
Debugging tips
Add debugPrint in each callback and watch which fires. Also try debugPrintGestureArenaDiagnostics = true;, which is defined in package:flutter/gestures.dart. It prints every arena event (added, accepted, rejected, swept) to the console, so you can see which recognizer won and why.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
void main() {
debugPrintGestureArenaDiagnostics = true;
runApp(const MaterialApp(home: Scaffold(body: Center(child: Text('Touch me')))));
}In widget tests the same arena runs. tester.tap sends a down and an up event, so a tap in a test resolves through the sweep exactly as on a device. tester.drag sends moves past the slop, so a drag test reliably triggers drag recognizers.
Exercise
A card has onTap and onDoubleTap on one GestureDetector. Users say single taps feel slow. Why does this happen, and what are two ways to fix it?
Show answer
The double tap recognizer keeps the arena open for about 300 ms after the first pointer up, in case a second tap follows. The tap recognizer cannot win until that timeout passes. Fix it by removing onDoubleTap if you do not need it, or by moving the double tap behavior to a separate gesture such as a long press so that single taps can resolve immediately.
Exercise
Two nested GestureDetectors both set onTap. You tap the inner widget. Which callback runs, and why?
Show answer
Only the inner callback runs. Hit testing adds the deepest widget's recognizer to the arena first. Nobody claims victory before pointer up, so the sweep awards the win to the first recognizer in the arena, which is the inner one.
Exercise
You place a horizontal swipe widget inside a vertical ListView. A user swipes diagonally and the list scrolls instead of your widget. Which callback choice makes this more predictable, and what role does the slop play?
Show answer
Use an axis-specific callback such as onHorizontalDragUpdate, not onPanUpdate. Drag recognizers accept only after the pointer passes the slop distance, and the recognizer whose axis matches the dominant direction wins. A mostly horizontal movement then goes to your widget, and a mostly vertical one scrolls the list.