debounce method
- Duration duration,
- dynamic onDebounce(), {
- @Deprecated("Please use the 'type' parameter instead.") bool isLeadingEdge = false,
- BehaviorType type = BehaviorType.trailingEdge,
Debounce the provided callback function by canceling any existing
timer and scheduling a new timer to invoke the callback after the
specified duration of inactivity.
The callback function is invoked only once, even if this method is
called multiple times within the duration.
The type parameter specifies the debounce behavior. Use BehaviorType.trailingEdge
to execute the onDebounce function after the duration has passed without
any new calls. Use BehaviorType.leadingEdge to execute the onDebounce
function immediately on the first call and postpone any subsequent calls
within the duration. Use BehaviorType.leadingAndTrailing to execute
the onDebounce function both on the leading and trailing edges of the
timer.
isLeadingEdge is deprecated and may not have the desired flexibility.
It is recommended to use the type parameter instead.
Implementation
void debounce(
Duration duration,
Function() onDebounce, {
@Deprecated("Please use the 'type' parameter instead.") bool isLeadingEdge = false,
BehaviorType type = BehaviorType.trailingEdge,
}) {
if (type == BehaviorType.leadingEdge || type == BehaviorType.leadingAndTrailing) {
onDebounce();
}
_debounceTimer?.cancel();
_debounceTimer = Timer(duration, () {
if (type == BehaviorType.trailingEdge || type == BehaviorType.leadingAndTrailing) {
onDebounce();
}
});
}