I have two containers in a stack and both containers have GestureDetector.The OnTap for the first container is working fine but it\'s not working with another container. The
Try setting the behavior
property of GestureDetector
to HitTestBehavior.translucent
.
I see that you are calling the callback method instead of assigning it to onTap property of GestureDetector widget. Pass the name of the method don't call it there.
Ok on my side it was quite stupid but since it may happens to many of us I will explain:
My build context (simplified for the example) tree was this way:
Widget build(BuildContext context) {
return (...) (
child: GestureDetector(
onTap: () => _myFunction(),
child: Container(
height: 64.0,
width: 128.0,
child: (...)
Seems legit right? But then I inspected into more details the bottom of my tree:
(...)
onTap: () => _myFunction(),
child: Container(
height: 64.0,
width: 128.0,
child: TheSourceOfEvil() // <-- whoopsie!
Quickly inspected TheSourceOfEvil to find out that the culprit was in fact ANOTHER GestureDetector has a child, that I had not yet implemented onTap: () => {},
:
Widget TheSourceOfEvil( {
return (...) (
child: GestureDetector(
onTap: () => {}, // override previous GestureDetector
child: Container(
child: (...);
Got rid of it and it instantly fixed my puzzling issue. I hope it will help!
For me it was an issue with items overflowing outside a Stack
, with clipBehavior: Clip.none
.
In that case the item is visible but not touchable. So I updated my layout to remove the clipBehavior: Clip.none
on my Stack
and the GestureDetector
started working.