Reputation: 4841
Is there any way to make a GestureDetector override the functionality of all children GestureDetectors?
I have a complex Widget that I would like to be able to easily override all of its behavior at a high level. For example, locking a free user out of functionality.
Upvotes: 13
Views: 11394
Reputation: 815
Another method: create an onTap attribute then pass that in to the child widget:
// Somewhere in parent widget
ChildWidget(onTap: () => doSomething())
class ChildWidget extends StatelessWidget {
ChildWidget({Key key, this.onTap}) : super(key: key);
final Function onTap;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
onTap();
},
child: SomeWidget(),
);
}
}
You'd probably want to make onTap optional, and if onTap is null, don't wrap in a GestureDetector.
Upvotes: -2
Reputation: 30103
To temporarily disable all child gesture detectors, use an IgnorePointer
widget:
@override
Widget build(BuildContext context) {
bool ignoreChildGestures = true;
return GestureDetector(
onTap: () {
print('parent tapped');
},
child: IgnorePointer(
ignoring: ignoreChildGestures,
child: GestureDetector(
onTapDown: (details) {
// won't be called when ignoring is set to true
print('child tap down!');
},
),
),
);
}
Upvotes: 22
Reputation: 277587
You can change HitTestBehavior behavior
of GestureDetector
to HitTestBehavior.opaque
GestureDetector(
behavior: HitTestBehavior.opaque,
...
)
By default, it uses HitTestBehavior.deferToChild
.
Upvotes: 7