positioned_tap_detector 1.0.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 75

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:

PositionedTapDetector(
  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,
        child: ...,
      ),
    ),
}

void _handleTap() {
  // ...
  _controller.onTap();
}

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.1] - 25/09/2018

  • Partially support nested GestureDetectors

[1.0.0] - 20/09/2018

  • Support onTap, onDoubleTap and onLongPress gestures

example/lib/main.dart

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

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              PositionedTapDetector(
                onTap: _onTap,
                onDoubleTap: _onDoubleTap,
                onLongPress: _onLongPress,
                child: Container(
                  width: 160.0,
                  height: 160.0,
                  color: Colors.redAccent,
                ),
              ),
              Padding(
                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:


dependencies:
  positioned_tap_detector: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages 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';
  
Version Uploaded Documentation Archive
1.0.1 Sep 25, 2018 Go to the documentation of positioned_tap_detector 1.0.1 Download positioned_tap_detector 1.0.1 archive
1.0.0+2 Sep 21, 2018 Go to the documentation of positioned_tap_detector 1.0.0+2 Download positioned_tap_detector 1.0.0+2 archive
1.0.0+1 Sep 21, 2018 Go to the documentation of positioned_tap_detector 1.0.0+1 Download positioned_tap_detector 1.0.0+1 archive
1.0.0 Sep 20, 2018 Go to the documentation of positioned_tap_detector 1.0.0 Download positioned_tap_detector 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
51
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on Oct 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4
  • Flutter: 0.9.5

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/positioned_tap_detector.dart.

Run flutter format to format lib/positioned_tap_detector.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test