Skip to content

Latest commit

 

History

History
68 lines (59 loc) · 1.78 KB

useCounter.md

File metadata and controls

68 lines (59 loc) · 1.78 KB

useCounter and useNumber

Flutter state hook that tracks a numeric value. useNumber is an alias for useCounter.

Installation

dependencies:
  flutter_use: ^0.0.2

Usage

class Sample extends HookWidget {
  @override
  Widget build(BuildContext context) {
    
    final action = useCounter(1, min: 1, max: 10);

    return Wrap(
      children: [
        Text(
            "Counter: ${action.value} [min: ${action.min}, max: ${action.max}]"),
        ElevatedButton(
          child: const Text('inc()'),
          onPressed: () => action.inc(),
        ),
        ElevatedButton(
          child: const Text('inc(5)'),
          onPressed: () => action.inc(5),
        ),
        ElevatedButton(
          child: const Text('dec()'),
          onPressed: () => action.dec(),
        ),
        ElevatedButton(
          child: const Text('dec(5)'),
          onPressed: () => action.dec(5),
        ),
        ElevatedButton(
          child: const Text('set(100)'),
          onPressed: () => action.set(100),
        ),
        ElevatedButton(
          child: const Text('reset()'),
          onPressed: () => action.reset(),
        ),
        ElevatedButton(
          child: const Text('reset(5)'),
          onPressed: () => action.reset(5),
        ),
      ]
    );
  }
}

Reference

  • value: int - current counter value;
  • get: int - getter of current counter value;
  • inc(int?) - increment current value;
  • dec(int?) - decrement current value;
  • set(int) - set arbitrary value;
  • reset(int?) - as the set, but also will assign value by reference to the initial parameter;