Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter detect triple tap

Tags:

flutter

I would like to be able to detect a triple tap (or even more) in a Flutter widget, although GestureDetector only has detection for double-tap built in.

What is the easiest way for me to detect a triple tap on a widget?

(I want continually clicking on a part of the screen to unlock some developer options)

like image 962
BGH Avatar asked Jan 26 '23 10:01

BGH


2 Answers

Was a bit lazy with this one, in reality it's not that hard

// init
int lastTap = DateTime.now().millisecondsSinceEpoch;
int consecutiveTaps = 0;

GestureDetector(
        onTap: () {
          int now = DateTime.now().millisecondsSinceEpoch;
          if (now - lastTap < 1000) {
            print("Consecutive tap");
            consecutiveTaps ++;
            print("taps = " + consecutiveTaps.toString());
            if (consecutiveTaps > 4){
              // Do something
            }
          } else {
            consecutiveTaps = 0;
          }
          lastTap = now;
        },
        child: ...
)
like image 118
BGH Avatar answered Jan 27 '23 23:01

BGH


I tried the method mentioned here, but it didn't work for me. GestureDetector onTap is called only once, regardless of the number of taps. Probably something has changed in flutter (I'm on the beta channel). However, I dug into the source code of flutter and come to the solution (https://api.flutter.dev/flutter/gestures/SerialTapGestureRecognizer-class.html):

import "package:flutter/gestures.dart";

RawGestureDetector(gestures: {
      SerialTapGestureRecognizer:
        GestureRecognizerFactoryWithHandlers<SerialTapGestureRecognizer>(
          () =>SerialTapGestureRecognizer(), (SerialTapGestureRecognizer instance) {
        instance.onSerialTapDown = (SerialTapDownDetails details) {
          if (details.count == 3) print("Consecutive tap 3");
        };
      })
like image 33
kernel Avatar answered Jan 28 '23 01:01

kernel