positioned_tap_detector 1.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 97

positioned-tap-detector #

pub package

Flutter widget allowing to receive tap callbacks, together with their position on the screen. It supports onTap, onDoubleTap and onLongPress gestures. Each callback function is invoked with TapPosition object that provides global and relative touch position. To adjust maximum time allowed between double-tap gesture consecutive taps, specify an additional doubleTapDelay parameter:

  onTap: (position) => _printTap('Single tap', position),
  onDoubleTap: (position) => _printTap('Double tap', position),
  onLongPress: (position) => _printTap('Long press', position),
  doubleTapDelay: Duration(milliseconds: 500),
  child: ...,

void _printTap(String gesture, TapPosition position) => 
    print('$gesture: ${position.global}, ${position.relative}');

PositionedTapDetector demo

Controller #

In case you need to wrap another GestureDetector below in the widget tree, pass an additional PositionedTapController parameter and invoke its callback methods whenever relevant gesture takes place:

final _controller = PositionedTapController();

Widget build(BuildContext context) {
  // ...
    child: PositionedTapDetector(
      // ...
      controller: _controller,
      child: GestureDetector(
        onTap: _handleTap,
        onTapDown: _controller.onTapDown,
        onLongPress: _controller.onLongPress,
        behavior: ...,
        child: ...,

void _handleTap() {
  // ...

Drawbacks #

PositionedTapDetector will not invoke onDoubleTap callback in case there's GestureDetector underneath it in the widget tree that also specifies onDoubleTap parameter. Since Flutter framework doesn't invoke its onTapDown callback when detector is double-tapped, positioned detector is unable to receive tap positions and therefore detect double-tap gestures.

[1.0.3] - 11/07/2019 #

  • Tap controller internal improvements
  • Tap controller bug fix

[1.0.2] - 14/12/2018 #

  • Add HitTestBehavior field

[1.0.1] - 25/09/2018 #

  • Partially support nested GestureDetectors

[1.0.0] - 20/09/2018 #

  • Support onTap, onDoubleTap and onLongPress gestures


import 'package:flutter/material.dart';
import 'package:positioned_tap_detector/positioned_tap_detector.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  MyAppState createState() => new MyAppState();

class MyAppState extends State<MyApp> {
  String _gesture = "";
  TapPosition _position = TapPosition(Offset.zero, Offset.zero);

  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
                onTap: _onTap,
                onDoubleTap: _onDoubleTap,
                onLongPress: _onLongPress,
                child: Container(
                  width: 160.0,
                  height: 160.0,
                  color: Colors.redAccent,
                padding: EdgeInsets.only(top: 16.0),
                child: Text("Gesture: $_gesture\n"
                    "Global: ${_formatOffset(_position.global)}\n"
                    "Relative: ${_formatOffset(_position.relative)}"),

  void _onTap(TapPosition position) => _updateState('single tap', position);

  void _onDoubleTap(TapPosition position) =>
      _updateState('double tap', position);

  void _onLongPress(TapPosition position) =>
      _updateState('long press', position);

  void _updateState(String gesture, TapPosition position) {
    setState(() {
      _gesture = gesture;
      _position = position;

  String _formatOffset(Offset offset) =>
      "${offset.dx.toStringAsFixed(1)}, ${offset.dy.toStringAsFixed(1)}";

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  positioned_tap_detector: ^1.0.3

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:positioned_tap_detector/positioned_tap_detector.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.7
  • Flutter: 1.12.13+hotfix.9


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies