Bloc Extensions
Packages
| Package | Version |
|---|---|
| action_bloc | |
| action_bloc_test | |
| flutter_action_bloc | |
| flutter_action_bloc_hooks | |
| flutter_bloc_hooks |
Bloc Hooks
Bloc hooks provide an easy way to use blocs from HookWidgets and other hooks without needing to access the current BuildContext or unnecessary deep nesting using BlocBuilders
and BlocListeners.
Example
class CounterText extends HookWidget {
@override
Widget build(BuildContext context) {
final counter = useBlocState<CounterCubit, int>();
return Text('$counter');
}
}
Action Blocs
ActionBlocs are bloc with an additional stream for actions (side effects). When using sealed classes (for example with freezed) as state side effects cannot easily be modeled
inside the state. Examples for such side effects include showing error messages or navigating to another screen.
Example
class CounterCubit extends ActionCubit<int, String> {
CounterCubit() : super(0);
void increment() {
emit(state + 1);
if (state % 15 == 0) {
emitAction('FizzBuzz');
} else if (state % 5 == 0) {
emitAction('Buzz');
} else if (state % 3 == 0) {
emitAction('Fizz');
}
}
}
class CounterText extends HookWidget {
const CounterText({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final counter = useBlocState<CounterCubit, int>();
useBlocActionListener<CounterCubit, String>((context, action) {
final snackBar = SnackBar(
content: Text(action),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
});
return Text('$counter');
}
}
Contributing
This repository uses melos for managing all packages. To get started first install melos and then run it’s bootstrap command.
dart pub global activate melos
melos bootstrap
More information about melos can be found on https://melos.invertase.dev. All available scripts can be seen in melos.yaml.