avoid-unnecessary-gesture-detector

Path
lint/performance/avoid-unnecessary-gesture-detector
Default
Off, opt in
Origin
Falcon
Domain
flutter
Rule
162 of 512
flutter

Flags handler-free Flutter GestureDetectors and tap-only wrappers around enabled Flutter buttons. Explicit hit-test behavior is retained. The button case requires an inline onPressed callback, so nullable handlers stay unflagged.

Invalid

example.dartdart
import 'package:flutter/widgets.dart';
import 'package:flutter/widgets.dart' as f;
typedef Detector = GestureDetector;
void examples(Widget child) {
  GestureDetector();
  GestureDetector(child: child);
  f.GestureDetector(child: TextButton());
  Detector(onTap: null, child: child);
  new GestureDetector(onLongPress: null);
  GestureDetector(onTap: () {}, child: TextButton(onPressed: () {}));
  GestureDetector(onTap: () {}, child: f.ElevatedButton(onPressed: () {}));
  Detector(onTap: () {}, child: OutlinedButton(onPressed: () {}));
  GestureDetector(onTap: () {}, child: IconButton(onPressed: () {}));
  GestureDetector(behavior: null, child: child);
}
Remove this `GestureDetector` or provide an event handler.
4void examples(Widget child) {
5 GestureDetector();
Remove this `GestureDetector` or provide an event handler.
5 GestureDetector();
6 GestureDetector(child: child);
Remove this `GestureDetector` or provide an event handler.
6 GestureDetector(child: child);
7 f.GestureDetector(child: TextButton());
Remove this `GestureDetector` or provide an event handler.
7 f.GestureDetector(child: TextButton());
8 Detector(onTap: null, child: child);
Remove this `GestureDetector` or provide an event handler.
8 Detector(onTap: null, child: child);
9 new GestureDetector(onLongPress: null);
Handle taps in the button's `onPressed` callback instead of wrapping it in `GestureDetector`.
9 new GestureDetector(onLongPress: null);
10 GestureDetector(onTap: () {}, child: TextButton(onPressed: () {}));
Handle taps in the button's `onPressed` callback instead of wrapping it in `GestureDetector`.
10 GestureDetector(onTap: () {}, child: TextButton(onPressed: () {}));
11 GestureDetector(onTap: () {}, child: f.ElevatedButton(onPressed: () {}));
Handle taps in the button's `onPressed` callback instead of wrapping it in `GestureDetector`.
11 GestureDetector(onTap: () {}, child: f.ElevatedButton(onPressed: () {}));
12 Detector(onTap: () {}, child: OutlinedButton(onPressed: () {}));
Handle taps in the button's `onPressed` callback instead of wrapping it in `GestureDetector`.
12 Detector(onTap: () {}, child: OutlinedButton(onPressed: () {}));
13 GestureDetector(onTap: () {}, child: IconButton(onPressed: () {}));
Remove this `GestureDetector` or provide an event handler.
13 GestureDetector(onTap: () {}, child: IconButton(onPressed: () {}));
14 GestureDetector(behavior: null, child: child);

Valid

example.dartdart
import 'package:flutter/widgets.dart';
void callback() {}
void examples(Widget child, void Function()? handler) {
  GestureDetector(onTap: callback, child: child);
  GestureDetector(onLongPress: callback);
  GestureDetector(onDoubleTap: callback, child: TextButton());
  GestureDetector(onTap: handler);
  GestureDetector(onTap: null, onLongPress: callback);
  GestureDetector(behavior: HitTestBehavior.opaque, child: SizedBox.expand());
  GestureDetector(behavior: HitTestBehavior.translucent, child: child);
  GestureDetector(onLongPress: callback, child: TextButton(onPressed: () {}));
  GestureDetector(onDoubleTap: callback, child: TextButton(onPressed: () {}));
  GestureDetector(onTap: callback, child: TextButton(onPressed: null));
  GestureDetector(onTap: callback, child: TextButton(onPressed: handler));
  GestureDetector(onTap: callback, onLongPress: callback, child: TextButton(onPressed: () {}));
  GestureDetector(behavior: HitTestBehavior.opaque, onTap: callback, child: TextButton(onPressed: () {}));
}

Configure avoid-unnecessary-gesture-detector

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "performance": {
        "avoid-unnecessary-gesture-detector": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/performance/avoid-unnecessary-gesture-detector: <reason>