Daniel Gomez Rico
Daniel Gomez Rico

Reputation: 15936

How to wrap GestureDetector with another GestureDetector and get events everywhere

If I have a GestureDetector that have an internal GestureDetector how can I setup it so that both detectors receive the click event?

You can see the running code here: https://dartpad.dev/37807a51a48e52eda81c24cf67260c33

GestureDetector(
      onTap: () => print("Log 1"),
      child: GestureDetector(
        onTap: () => print("Log 2"),
        child: Text("CLICK ME")
      )
);

Upvotes: 7

Views: 2920

Answers (2)

Mirzo-Golib
Mirzo-Golib

Reputation: 134

There is also a simpler way to achieve this by using Listener:

Listener(
 onPointerDown: (event) => print("Log 1"),
 child: GestureDetector(
  onTap: () => print("Log 2"),
  child: Text("CLICK ME"),
 ),
);

Hope this solves this issue!

Upvotes: 2

CharlyKeleb
CharlyKeleb

Reputation: 754

    class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return RawGestureDetector(
        gestures: {
          AllowMultipleVerticalDragGestureRecognizer: GestureRecognizerFactoryWithHandlers<
              AllowMultipleVerticalDragGestureRecognizer>(
            () => AllowMultipleVerticalDragGestureRecognizer(),
            (AllowMultipleVerticalDragGestureRecognizer instance) {
              instance..onEnd = (_) => print("test1");
            },
          )
        },
        child: RawGestureDetector(
          gestures: {
            AllowMultipleVerticalDragGestureRecognizer: GestureRecognizerFactoryWithHandlers<
                AllowMultipleVerticalDragGestureRecognizer>(
              () => AllowMultipleVerticalDragGestureRecognizer(),
              (AllowMultipleVerticalDragGestureRecognizer instance) {
                instance..onEnd = (_) => print("test2");
              },
            )
          },
          child: Container(color: Colors.red),
        ));
  }
}

class AllowMultipleVerticalDragGestureRecognizer extends VerticalDragGestureRecognizer{
  @override
  void rejectGesture(int pointer) {
    acceptGesture(pointer);
  }
}

Credit: https://gist.github.com/Nash0x7E2/08acca529096d93f3df0f60f9c034056

Upvotes: 5

Related Questions