fluttergesturedetector

Difference between HitTestBehavior.opaque and HitTestBehavior.translucent? (GestureDetector)


return GestureDetector(
    onTap: () => Navigator.of(context).pop(),
    child: GestureDetector(
      behavior: HitTestBehavior.translucent, <=== SEE HERE
      onTap: () {},
      child: child,
    ),
  );

I don't understand the difference between opaque and tanslucent. Based on my code both of them don't work


Solution

  • The HitTestBehavior.translucent allows you to also trigger the Widget behind your top Z-Index widget. The HitTestBehavior.opaque will only trigger the top Z-index widget.

    Imagine you have a Stack with two GestureDetector. the .opaque will trigger only the first one and the .translucent will trigger both.

    I don't really understand why you need 2 GestureDetector here. Maybe try something like this:

    return GestureDetector(
        onTap: () => Navigator.of(context).pop(),
        behavior: HitTestBehavior.translucent, <=== SEE HERE
        child: child
      );
    

    In your code the 2 GestureDetector are listening to the same Gesture and only one will trigger the action. Here it's the second one who triggers the actions empty function