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

后端 未结 3 1204
自闭症患者
自闭症患者 2021-02-20 05:24

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 clic

相关标签:
3条回答
  • 2021-02-20 06:00

    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

    0 讨论(0)
  • 2021-02-20 06:03

    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),
    )
    
    0 讨论(0)
  • 2021-02-20 06:04

    Wrap it in an AbsorbPointer

    var shouldAbsorb = true;
    
    AbsorbPointer(
        absorbing: shouldAbsorb,
        child: GestureDetector(
          onTap: () {
            Navigator.of(context).pop();
          },
          child: Container(
            color: Colors.red,
          ),
        ),
      )
    
    0 讨论(0)
提交回复
热议问题