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.
Why use this rule
Section titled “Why use this rule”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'), ),];Configuration
Section titled “Configuration”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:
rules: avoid_unnecessary_gesture_detector: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”always_pass_global_key— Don’t create a GlobalKey inside build.avoid_conditional_hooks— Never call hooks inside conditionals, loops, or ternaries.avoid_deep_widget_nesting— Keep a widget tree within a nesting budget.avoid_flexible_outside_flex— Only use Flexible and Expanded as direct children of Row, Column, or Flex.