How do I get tap locations (GestureDetector)?

0

Issue

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?

Solution

You can provide a GestureTapDownCallback callback as the onTapDown argument of the GestureDetector constructor. The GestureTapDownCallback takes a TapDownDetails argument that includes the global position of the tap. You can then convert it to relative coordinates using BuildContext.findRenderObject and RenderBox.globalToLocal:

Offset _tapPosition;

void _handleTapDown(TapDownDetails details) {
  final RenderBox referenceBox = context.findRenderObject();
  setState(() {
    _tapPosition = referenceBox.globalToLocal(details.globalPosition);
  });
}

@override
Widget build(BuildContext context) {
  return new GestureDetector(
     /* ... */
     onTapDown: _handleTapDown,
  );
}

In your onDoubleTap handler, you can reference _tapPosition to find out where the most recent tap was located.

For an example of this in action, see InkWell.

Answered By – Collin Jackson

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More