Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: How to disable onTap for a while in gesture detector?

Tags:

flutter

dart

I have a GestureDetector in a custom stateless view. when onTap triggered I show a snack bar that displays some info. When the user makes multiple clicks fast it keeps displaying the snack bar forever.

source code

GestureDetector(
  onTap: () {  
    Clipboard.setData(new ClipboardData(text: idText));
    Scaffold.of(context).showSnackBar(SnackBar
      (content: Text('ID copied')));
  },
  child: Icon(Icons.content_copy,),
}

I want to disable the onTap for seconds before it can be clicked again.

like image 206
Ammar Hussein Avatar asked Apr 06 '19 08:04

Ammar Hussein


People also ask

How do I disable onTap?

You can use the httpd. enable option to enable or disable the HTTP server that is built into Data ONTAP. By default, this option is off.

How do you use GestureDetector in flutter?

To identify a gesture targeted on a widget, the widget can be placed inside GestureDetector widget. GestureDetector will capture the gesture and dispatch multiple events based on the gesture. Now, let us modify the hello world application to include gesture detection feature and try to understand the concept.

How do I disable widgets on flutter?

In flutter, most widgets already come with an option to disable them for example in a RaisedButton we can set the onClicked function to null to disable, or we can use NeverScrollableScrollPhysics( ) to disable a ListView.

How do you find the long press in flutter?

Use a GestureDetector to capture long-press. Use the function showMenu() to display a popup menu, and position it near the finger's touch.


3 Answers

Create a bool flag and define a method:

bool _enabled = true; 

void _onTap () {
  // Disable GestureDetector's 'onTap' property.  
  setState(() => _enabled = false);
  
  // Enable it after 1s.
  Timer(Duration(seconds: 1), () => setState(() => _enabled = true));
  
  // Rest of your code...          
}

Usage:

GestureDetector(
  onTap: _enabled ? _onTap: null,
  child: Icon(Icons.content_copy),
)
like image 170
CopsOnRoad Avatar answered Oct 17 '22 16:10

CopsOnRoad


Wrap it in an AbsorbPointer

var shouldAbsorb = true;

AbsorbPointer(
    absorbing: shouldAbsorb,
    child: GestureDetector(
      onTap: () {
        Navigator.of(context).pop();
      },
      child: Container(
        color: Colors.red,
      ),
    ),
  )
like image 25
Boy Avatar answered Oct 17 '22 15:10

Boy


Personally I use two others methods from GestureDetector:

onTapDown: When user is pressing your widget.

onTapUp: When user leave the widget.

onTap: Is when tap down and tap up follows. If user slide his finger too much Flutter take this like a cancel tap.

onTapCancel: When user cancel.

bool pressing = false;

GestureDetector(
  // when user is pressing
  onTapDown: (details) {
    setState(() {
      pressing = true;
    });
  },
  // when user leaved
  onTapUp: (details) {
    setState(() {
      pressing = false;
    });
  },
  // when user leaved
  onTapCancel: () {
    setState(() {
      pressing = false;
    });
  }
  // the action to do when user tap
  onTap: () {
    // code...
  }
);

Here is the documentation: Flutter GestureDetector Documentation

like image 3
BossSuricate Avatar answered Oct 17 '22 14:10

BossSuricate