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

后端 未结 3 1207
自闭症患者
自闭症患者 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: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),
    )
    

提交回复
热议问题