avoid-unnecessary-gesture-detector
- Group
- Performance
- Path
lint/performance/avoid-unnecessary-gesture-detector- Default
- Off, opt in
- Origin
- Falcon
- Domain
- flutter
- Rule
- 162 of 512
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
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);
}void examples(Widget child) { GestureDetector(); GestureDetector(); GestureDetector(child: child); GestureDetector(child: child); f.GestureDetector(child: TextButton()); f.GestureDetector(child: TextButton()); Detector(onTap: null, child: child); Detector(onTap: null, child: child); new GestureDetector(onLongPress: null); new GestureDetector(onLongPress: null); GestureDetector(onTap: () {}, child: TextButton(onPressed: () {})); GestureDetector(onTap: () {}, child: TextButton(onPressed: () {})); GestureDetector(onTap: () {}, child: f.ElevatedButton(onPressed: () {})); GestureDetector(onTap: () {}, child: f.ElevatedButton(onPressed: () {})); Detector(onTap: () {}, child: OutlinedButton(onPressed: () {})); Detector(onTap: () {}, child: OutlinedButton(onPressed: () {})); GestureDetector(onTap: () {}, child: IconButton(onPressed: () {})); GestureDetector(onTap: () {}, child: IconButton(onPressed: () {})); GestureDetector(behavior: null, child: child);Valid
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.
{
"linter": {
"rules": {
"performance": {
"avoid-unnecessary-gesture-detector": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/performance/avoid-unnecessary-gesture-detector: <reason>More performance rules
- avoid-unnecessary-hook-widgetsflutter_hooks
Flags confirmed hook widgets and
HookBuildercallbacks that do not use hooks. - move-variable-closer-to-its-usage
Move final/const scalar literals into their only branch, or after an if.
- move-variable-outside-iteration
Reports loop-local final variables whose computation is independent of the loop.
- noop-primitive-operations
noop_primitive_operationsrecommendedFlags primitive operations that return their receiver unchanged.
- prefer-const-border-radius
prefer-const-border-radiusrecommendedflutterFlags
BorderRadius.only(...)whose four corner radii are all equal. - prefer-contains
prefer_containsrecommendedPrefers
containsto comparing the result ofindexOfwith a constant. - prefer-correct-edge-insets-constructor
prefer-correct-edge-insets-constructorrecommendedflutterFlags an
EdgeInsets.only(...)that a more specific constructor expresses better. - prefer-declaring-const-constructor
prefer_declaring_const_constructorflutterFlags a generative constructor that is legally eligible to be
const.