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

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

To disable this rule:

plugins:
many_lints:
diagnostics:
avoid_unnecessary_gesture_detector: false