Home > Net >  Is it possible to pass a tap event to multiple widgets at the same time in Flutter?
Is it possible to pass a tap event to multiple widgets at the same time in Flutter?

Time:08-28

When I tap a widget, I want that widget and the widget behind it to recognize my tap at the same time. But what happens is only the widget at forefront recognizes the tap.

I've tried this in several ways, with a simple parent/child relation with all the combinations of HitTestBehavior:

Center(
  child: GestureDetector(
    behavior: HitTestBehavior.opaque,
    onTap: () => print('red'),
    child: Container(
      color: Colors.red,
      width: 200,
      height: 200,
      child: Center(
        child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () => print('green'),
          child: Container(
            color: Colors.green,
            width: 100,
            height: 100,
          ),
        ),
      ),
    ),
  ),
);

Also tried using a Stack. Again, with all possible behaviors:

Stack(
  alignment: Alignment.center,
  children: [
    GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () => print('red'),
      child: Container(
        color: Colors.red,
        width: 200,
        height: 200,
      ),
    ),
    GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () => print('green'),
      child: Container(
        color: Colors.green,
        width: 100,
        height: 100,
      ),
    ),
  ],
);

I know I can simply use a mutual function for both of the detectors, but the main reason I want to achieve this is when using nested widgets, it is required to add unnecessary implementations to access that functionality.

CodePudding user response:

You can use onPanDown

GestureDetector(
  behavior: HitTestBehavior.translucent,
  onTap: () => print('green'),//doesnt work
  onPanDown: (details) => print('tdgreen'),
  child: Container(
    color: Colors.green,
    width: 100,
    height: 100,
  ),
),

You can check this thread for more.

  • Related