SmartDebouncer
A smart debouncer for Flutter that dynamically adjusts its delay based on the user's actual typing speed using the Exponential Moving Average (EMA) algorithm.
Unlike a traditional debouncer with a hardcoded delay, SmartDebouncer continuously learns the user's typing rhythm and adapts accordingly â fast typers get shorter delays, slow typers get longer ones.
âĻ Features
- ð§ Adaptive delay â Automatically adjusts based on typing speed via EMA
- âąïļ Pause detection â Ignores natural pauses between words/thoughts
- ð Safety bounds â Delay is always clamped between
minDelayandmaxDelay - ðŠķ Zero dependencies â Pure Dart, no external packages required
- ðą Flutter-ready â Designed for
TextFieldautocomplete/search use cases
ðĶ Installation
Add to your pubspec.yaml:
dependencies:
smart_debouncer:
path: ../smart_debouncer # or from pub.dev
ð Quick Start
import 'package:smart_debouncer/smart_debouncer.dart';
final debouncer = SmartDebouncer();
TextField(
onChanged: (value) {
debouncer.run(() {
searchApi(value);
});
},
);
// Don't forget to dispose!
@override
void dispose() {
debouncer.dispose();
super.dispose();
}
âïļ Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
minDelay |
int |
150 |
Minimum debounce delay (ms) |
maxDelay |
int |
800 |
Maximum debounce delay (ms) |
alpha |
double |
0.3 |
EMA smoothing factor (0.0â1.0) |
pauseThreshold |
int |
1500 |
Pause detection threshold (ms) |
multiplier |
double |
1.5 |
Multiplier applied to EMA for final delay |
final debouncer = SmartDebouncer(
minDelay: 200,
maxDelay: 1000,
alpha: 0.4,
pauseThreshold: 2000,
multiplier: 1.8,
);
ð§Ū How It Works
-
EMA Update: On each keystroke, the interval is smoothed:
currentEma = (interval à alpha) + (currentEma à (1 - alpha)) -
Dynamic Delay: The debounce delay is calculated as:
dynamicDelay = currentEma à multiplier -
Safety Clamp: The delay is clamped:
minDelay âĪ dynamicDelay âĪ maxDelay -
Pause Filter: Intervals >
pauseThresholdare ignored (natural pauses)
ð License
MIT License â see LICENSE for details.
Libraries
- smart_debouncer
- A smart debouncer that dynamically adjusts delay using Exponential Moving Average (EMA) based on actual user typing speed.