Skip to content

avoid_unnecessary_gesture_detector

v0.4.0 Warning Fix Widget Best Practices

This rule flags GestureDetector widgets that have no event handler callbacks (no onTap, onLongPress, onDoubleTap, etc.). A GestureDetector without any handlers does nothing useful but still participates in hit testing, which can interfere with gesture recognition for widgets below it.

A handler-less GestureDetector is dead code that adds clutter to the widget tree. It may also unintentionally swallow touch events from child widgets, especially when behavior is set to HitTestBehavior.opaque. Removing it or adding the intended handler makes the code correct and easier to understand.

See also: GestureDetector | InkWell

final widgets = <Widget>[
// GestureDetector without any on* callback
GestureDetector(child: const Text('hello')),
// Non-handler arguments like behavior don't count
GestureDetector(
behavior: HitTestBehavior.opaque,
child: const Text('world'),
),
];
final widgets = <Widget>[
GestureDetector(
onTap: () => print('tapped'),
child: const Text('hello'),
),
GestureDetector(
onLongPress: () => print('long pressed'),
onDoubleTap: () => print('double tapped'),
child: const Text('world'),
),
];

This rule is in the recommended preset, so it is on with preset: recommended or preset: opinionated. Add it to preset: core with avoid_unnecessary_gesture_detector: true.

To turn it off:

many_lints.yaml
rules:
avoid_unnecessary_gesture_detector: false

To keep the rule on but skip certain paths, use per-rule exclude.