Skip to content

Latest commit

 

History

History
63 lines (53 loc) · 1.75 KB

useTimeoutFn.md

File metadata and controls

63 lines (53 loc) · 1.75 KB

useTimeoutFn

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.

Installation

dependencies:
  flutter_use: ^0.0.2

Usage

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"),
            ),
          ],
        ),
      ),
    );
  }
}

Reference

  • fn: VoidCallback - function that will be called;
  • delay: Duration - delay
  • isReady(): bool? - function returning current timeout state:
    • false - pending re-build
    • true - re-build performed
    • null - re-build cancelled
  • cancel() - cancel the timeout (component will not be re-builded)
  • reset() - reset the timeout