Skip to content

Latest commit

 

History

History
48 lines (41 loc) · 1.03 KB

useToggle.md

File metadata and controls

48 lines (41 loc) · 1.03 KB

useBoolean and useToggle

Flutter state hook that tracks value of a boolean. useBoolean is an alias for useToggle.

Installation

dependencies:
  flutter_use: ^0.0.2

Usage

class Sample extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final toggleState = useToggle();

    return Column(
      children: [
        Text("toggle: ${state.value ? 'ON' : 'OFF'}"),
        ElevatedButton(
          onPressed: () {
            toggleState.toggle();
          },
          child: const Text('Toggle'),
        ),
        ElevatedButton(
          onPressed: () {
            toggleState.toggle(true);
          },
          child: const Text('set ON'),
        ),
        ElevatedButton(
          onPressed: () {
            toggleState.toggle(false);
          },
          child: const Text('set OFF'),
        ),
      ]
    );
  }
}