Skip to content

Latest commit

 

History

History
54 lines (45 loc) · 1.38 KB

useTimeout.md

File metadata and controls

54 lines (45 loc) · 1.38 KB

useTimeout

Re-builds the component after a specified duration. Provides handles to cancel and/or reset the timeout.

Installation

dependencies:
  flutter_use: ^0.0.2

Usage

class Sample extends HookWidget {
  @override
  Widget build(BuildContext context) {
    
    final timeout = useTimeout(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

  • 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