StateNotifier 钩子
这个包提供了一个类似于 useValueListenable 的 useStateNotifier 钩子。 还有
useCreateStateNotifier 钩子,它创建一个 StateNotifier 并自动释放它。
用法
// 1. Create your state notifier as usual.
class CounterNotifier extends StateNotifier<int> {
CounterNotifier() : super(0);
void increment() => state++;
void decrement() => state--;
}
// 2. Extend hook widget
class ReadMeExample extends HookWidget {
const ReadMeExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// 3. Create your notifier (useCreateStateNotifier will dispose it)
final notifier = useCreateStateNotifier(() => CounterNotifier());
// 4. Listen to your state
final state = useStateNotifier(notifier);
//......
}
}