Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Created October 16, 2019 08:25
Show Gist options
  • Save CaiJingLong/acb7db4632c06e6817f2de037cbb2113 to your computer and use it in GitHub Desktop.
Save CaiJingLong/acb7db4632c06e6817f2de037cbb2113 to your computer and use it in GitHub Desktop.
点击防抖动
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