Sub topic 8
Hit Testing and HitTestBehavior
Assume you know Dart closures, widget trees, StatefulWidget, setState, and basic layout widgets. This chapter explains how Flutter decides which widget receives a pointer event before a gesture callback can run.
What hit testing does
Hit testing is the process of finding widgets under a pointer location. When a user touches or clicks the screen, Flutter starts at the app's root and checks the rendered widget tree. It records the widgets that contain that position.
The result is a hit-test path. Gesture recognizers, such as the tap recognizer used by GestureDetector, use this path to compete for the event.
A widget can be visible without receiving events. For example, a transparent Container may not count as a hit unless it has a size and participates in hit testing.
Choosing HitTestBehavior
GestureDetector has a behavior property of type HitTestBehavior. It controls whether the detector itself counts as a hit.
| Behavior | Detector receives events when |
|---|---|
| `deferToChild` | A child receives the hit. This is the default. |
| `opaque` | The detector's entire layout area receives the hit. |
| `translucent` | The entire area receives the hit, while widgets behind it may also receive hits. |
opaque does not mean the widget must be painted with an opaque color. It means the detector claims the area for hit testing. This is useful when a transparent part of a layout should still respond.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: HitTestExample()));
}
class HitTestExample extends StatelessWidget {
const HitTestExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => debugPrint('The whole box was tapped'),
child: const SizedBox(
width: 220,
height: 120,
child: Center(child: Text('Tap anywhere in this box')),
),
),
),
);
}
}With the default deferToChild, only the Text area might respond because the child has the meaningful hit region. With opaque, the whole SizedBox responds.
Hit testing is different from gesture recognition
Hit testing finds possible targets. It does not decide which gesture wins. After hit testing, recognizers enter the gesture arena. A tap recognizer and a horizontal drag recognizer can both receive pointer information, but Flutter later accepts one and rejects the other.
Changing HitTestBehavior changes which widgets enter the path. It does not force a tap to win over a drag or make overlapping recognizers both invoke their callbacks.
Common mistakes
- Wrapping a widget in
GestureDetectorwithout giving the detector a useful layout size. - Expecting
opaqueto change the detector's color or visual appearance. - Using
translucentto solve gesture conflicts. It changes hit testing, not gesture-arena rules. - Placing an interactive widget behind another widget that claims the same pointer location.
- Forgetting that scrolling widgets have their own drag recognizers.
Exercise
A transparent 200 by 80 area should respond to taps, including empty space around its label. Which behavior should you use, and why?
Show answer
Use HitTestBehavior.opaque. It makes the detector's entire layout area count as a hit, even when the child does not paint over every pixel.