hooks_reduct 1.0.1 copy "hooks_reduct: ^1.0.1" to clipboard
hooks_reduct: ^1.0.1 copied to clipboard

A flutter hooks for reduct library, an elementary yet adaptable state management solution for Dart.

example/hooks_reduct_example.dart

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart' hide Reducer;
import 'package:hooks_reduct/hooks_reduct.dart';
import 'package:reduct/reduct.dart';

void main() {
  CounterReducer();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Hooks Reduct',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Hooks for Reduct'),
    );
  }
}

class MyHomePage extends HookWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  Widget build(BuildContext context) {
    useAtomListener(counterState, (count) {
      final snackBar = SnackBar(content: Text('Counter: $count'));
      ScaffoldMessenger.of(context).showSnackBar(snackBar);
    });

    final isOdd = useAtomSelector(counterState, (value) => value.isOdd);

    final counter = useAtom(counterState);

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(title),
      ),
      body: Center(
        child: Text(
          'Counter: $counter \nisOdd: $isOdd',
          style: Theme.of(context).textTheme.headlineMedium,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: incrementAction.call,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

// atoms
final counterState = Atom(0);
final incrementAction = Atom.action();

// reducer
class CounterReducer extends Reducer {
  CounterReducer() {
    on(incrementAction, (_) => counterState.value++);
  }
}
1
likes
160
pub points
53%
popularity

Publisher

verified publishereronsoft.com

A flutter hooks for reduct library, an elementary yet adaptable state management solution for Dart.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_hooks, reduct

More

Packages that depend on hooks_reduct