How do I get tap locations (GestureDetector)?

Viewed 5348

I'm trying to implement double-tap-to-zoom in my zoomable_images plugin but the GestureTapCallback doesn't provide the tap location information.

Ideally the offset would be returned by the callback. Is there another API for this?

3 Answers

as of [✓] Flutter (Channel stable, 2.5.3)

GestureDetector(
      onTapDown: (details) {

        var position = details.globalPosition;
        // you can also check out details.localPosition; 

        if (position.dx < MediaQuery.of(context).size.width / 2){
          // tap left side
        } else {
          // tap rigth size
        }
      },
      child: SomeChildWidget(),
),

If you want to handle double taps, you'll need to store the tap position from the onDoubleTapDown and then work with onDoubleTap:

late Offset _doubleTapPosition;
...
onDoubleTap: () {
    //do your stuff with _doubleTapPosition here
},
onDoubleTapDown: (details) {
    final RenderBox box = context.findRenderObject() as RenderBox;
    _doubleTapPosition = box.globalToLocal(details.globalPosition);
},

Original answer

Related