Calls given function after specified duration.
Several thing about it's work:
- does not re-build component;
- automatically cancel timeout on cancel;
- automatically reset timeout on delay change;
- reset function call will cancel previous timeout;
- timeout will NOT be reset on function change. It will be called within the timeout, you have to reset it on your own when needed.
dependencies:
flutter_use: ^0.0.2
class Sample extends HookWidget {
@override
Widget build(BuildContext context) {
final timeout = useTimeoutFn(() {
debugPrint("Timeout");
}, const Duration(milliseconds: 300));
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text("isReady?: ${timeout.isReady}"),
ElevatedButton(
onPressed: () => timeout.cancel(),
child: const Text("Cancel"),
),
ElevatedButton(
onPressed: () => timeout.reset(),
child: const Text("Reset"),
),
],
),
),
);
}
}
fn
: VoidCallback
- function that will be called;delay
: Duration
- delayisReady()
: bool?
- function returning current timeout state:false
- pending re-buildtrue
- re-build performednull
- re-build cancelled
cancel()
- cancel the timeout (component will not be re-builded)reset()
- reset the timeout