Maciej Pszczolinski
Maciej Pszczolinski

Reputation: 1958

Flutter GestureDetector is not capturing gestures if it's child is handling them

I have following widget:

GestureDetector(
  behavior: HitTestBehavior.opaque,
//  behavior: HitTestBehavior.translucent,
  onTap: (() {
    print('gesture detector tap');
  }),
  onLongPress: (() {
    print(
        'gesture detector longPress');
  }),
  child: ElevatedButton(
    onPressed: () {
      print('button pressed');
    },
    onLongPress: () {
      print('button long pressed');
    },
    child: Text('I am button'),
  ),
);

No matter if I go with HitTestBehavior.translucent or HitTestBehavior.opaque or HitTestBehavior.deferToChild it is always ONLY ElevatedButton's events that are fired.

Why is it like that?

I believe that with HitTestBehavior.opaque the GestureDetector should "intercept" the gestures, right?

Upvotes: 1

Views: 1383

Answers (2)

mcmah309
mcmah309

Reputation: 11

The flutter documentation for HitTestBehavior is wrong at worst and misleading at best and needs to be updated: https://github.com/flutter/flutter/issues/74733

Even the flutter team seems to be confused what exactly is happening.

But in this case, opaque does not do what you think it should do. Instead you need AbsorbPointer to stop the child from getting hit.

GestureDetector(
    child: AbsorbPointer(
        ...
    )
)

Upvotes: 0

IVλ-N
IVλ-N

Reputation: 41

Try replace GestureDetector on:

Listener(
    behavior: HitTestBehavior.translucent,
    onPointerDown: ..,
    onPointerUp: ..,
..)

Upvotes: 3

Related Questions