Skip to content

Latest commit

 

History

History
46 lines (37 loc) · 1.24 KB

useDebounce.md

File metadata and controls

46 lines (37 loc) · 1.24 KB

useDebounce

Flutter hook that delays invoking a function until after wait milliseconds have elapsed since the last time the debounced function was invoked.

The third argument is the array of values that the debounce depends on, in the same manner as useEffect. The debounce timeout will start when one of the values changes.

Installation

dependencies:
  flutter_use: ^0.0.2

Usage

class Sample extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final state = useState('Typing stopped');
    final inputValue = useState('');
    final bounceValue = useState('');
    
    useDebounce(() {
      state.value = 'Typing stopped';
      bounceValue.value = inputValue.value;
    }, const Duration(seconds: 1));

    return Column(
      children: [
        Text("Typing?: ${state.value}"),
        Text("Value: ${bounceValue.value}"),
        TextFormField(
          onChanged: (text) {
            state.value = 'Waiting for typing to stop...';
            inputValue.value = text;
            debugPrint(text);
          },
        ),
      ]
    );
  }
}