Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GestureDetector that overrides children GestureDetectors?

Tags:

flutter

Is there any way to make a GestureDetector override the functionality of all children GestureDetectors?

I have a complex Widget that I would like to be able to easily override all of its behavior at a high level. For example, locking a free user out of functionality.

like image 919
Luke Pighetti Avatar asked Jul 24 '18 17:07

Luke Pighetti


People also ask

What is the difference between InkWell and GestureDetector?

They both provide many common features like onTap , onLongPress etc. The main difference is GestureDetector provides more controls like dragging etc. on the other hand it doesn't include ripple effect tap, which InkWell does.

What is onTapDown?

onTapDown. A pointer that might cause a tap with a primary button has contacted the screen at a particular location. This is called after a short timeout, even if the winning gesture has not yet been selected. If the tap gesture wins, onTapUp will be called, otherwise onTapCancel will be called.

What is the use of gesture detector?

GestureDetector. OnGestureListener notifies users when a particular touch event has occurred. To make it possible for your GestureDetector object to receive events, you override the View or Activity's onTouchEvent() method, and pass along all observed events to the detector instance.

What is HitTestBehavior in flutter?

HitTestBehavior() opaque → const HitTestBehavior. Opaque targets can be hit by hit tests, causing them to both receive events within their bounds and prevent targets visually behind them from also receiving events. HitTestBehavior() translucent → const HitTestBehavior.


2 Answers

To temporarily disable all child gesture detectors, use an IgnorePointer widget:

  @override
  Widget build(BuildContext context) {

    bool ignoreChildGestures = true;

    return GestureDetector(
      onTap: () {
        print('parent tapped');
      },
      child: IgnorePointer(
        ignoring: ignoreChildGestures,
        child: GestureDetector(
          onTapDown: (details) {
            // won't be called when ignoring is set to true
            print('child tap down!');
          },
        ),
      ),
    );
  }
like image 157
boformer Avatar answered Oct 26 '22 17:10

boformer


You can change HitTestBehavior behavior of GestureDetector to HitTestBehavior.opaque

GestureDetector(
   behavior: HitTestBehavior.opaque,
   ...
)

By default, it uses HitTestBehavior.deferToChild.

like image 23
Rémi Rousselet Avatar answered Oct 26 '22 18:10

Rémi Rousselet