Sub topic 11
How Competing Gestures Are Resolved in the Gesture Arena
This chapter assumes basic Dart syntax, Flutter widget trees, GestureDetector, and setState. It also assumes you know how Row, Column, Stack, and Padding lay out widgets.
Why Flutter needs a gesture arena
A pointer can touch several widgets at the same location. For example, a vertical list may contain a horizontally draggable card. Both the list and the card may want to handle the same finger movement.
Flutter first performs hit testing to find widgets under the pointer. Each matching gesture recognizer enters a gesture arena for that pointer sequence. A gesture recognizer is an object that watches pointer events and decides whether they form a tap, drag, long press, or another gesture.
The recognizers compete:
- A recognizer can accept the gesture.
- A recognizer can reject the gesture.
- The first recognizer to accept usually wins.
- Once a recognizer wins, the other recognizers receive rejection.
- A recognizer that loses does not receive its normal gesture callback.
The arena prevents two incompatible gestures from acting on the same pointer sequence.
Direction decides many drag contests
Flutter's drag recognizers wait until the movement is large enough to determine its direction. A horizontal drag recognizer looks mainly at horizontal movement. A vertical drag recognizer looks mainly at vertical movement.
This allows a horizontal child inside a vertical scroll view to work naturally. A mostly horizontal movement can be won by the child. A mostly vertical movement can be won by the scroll view.
class DirectionalGestures extends StatelessWidget {
const DirectionalGestures({super.key});
@override
Widget build(BuildContext context) {
return GestureDetector(
onHorizontalDragStart: (_) {
debugPrint('Horizontal gesture won');
},
onVerticalDragStart: (_) {
debugPrint('Vertical gesture won');
},
child: Container(
width: 240,
height: 120,
alignment: Alignment.center,
color: Colors.indigo,
child: const Text(
'Drag horizontally or vertically',
style: TextStyle(color: Colors.white),
),
),
);
}
}The two callbacks above belong to different recognizers. They do not both run for one drag. The recognizer whose direction matches the movement wins the arena.
Parent and child recognizers
A parent and child can both participate because hit testing produces a path containing both widgets. This does not mean that the parent automatically handles the event first. The gesture arena decides after recognizers inspect the pointer sequence.
A child can prevent a parent from receiving a gesture by accepting it first. A parent can win when the child rejects the movement or when the parent’s recognizer accepts first.
Some recognizers can cooperate through a gesture arena team. This is useful when multiple recognizers should share victory, but it is an advanced choice. Most applications should first solve conflicts by choosing the correct gesture callbacks and widget structure.
> callout note > The gesture arena resolves recognizers for one pointer sequence. It is separate from visual layout and separate from semantic accessibility behavior.
Common mistakes
- Expecting both
onTapandonPanUpdateto run for the same interaction. - Placing a draggable child inside a scrollable without considering movement direction.
- Using a large parent
GestureDetectorthat unintentionally competes with every child. - Assuming callback order is a reliable way to control which recognizer wins.
- Forgetting that a recognizer may reject a gesture before its callback runs.
When a conflict is difficult to understand, temporarily log each recognizer's start and end callbacks. Then check the hit-test structure and the direction of movement.
Exercise
A vertical ListView contains a child with onHorizontalDragUpdate. Which recognizer should usually win when the user moves mostly horizontally?
Show answer
The child’s horizontal drag recognizer should usually win because its direction matches the pointer movement. The list’s vertical drag recognizer should reject the gesture.