Created
October 16, 2019 08:25
-
-
Save CaiJingLong/acb7db4632c06e6817f2de037cbb2113 to your computer and use it in GitHub Desktop.
点击防抖动
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class ThrottleWrapper extends StatefulWidget { | |
final Duration throttleDuration; | |
final Widget child; | |
final Function onTap; | |
const ThrottleWrapper({ | |
Key key, | |
this.throttleDuration, | |
this.child, | |
this.onTap, | |
}) : super(key: key); | |
@override | |
_ThrottleWrapperState createState() => _ThrottleWrapperState(); | |
} | |
class _ThrottleWrapperState extends State<ThrottleWrapper> { | |
bool ignore = false; | |
@override | |
Widget build(BuildContext context) { | |
return IgnorePointer( | |
ignoring: ignore, | |
child: GestureDetector( | |
onTap: _onTap, | |
child: widget.child, | |
), | |
); | |
} | |
void _onTap() async { | |
widget.onTap?.call(); | |
setState(() { | |
ignore = true; | |
}); | |
await Future.delayed(widget.throttleDuration); | |
setState(() { | |
ignore = false; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment