bonfire_bloc 0.0.2
bonfire_bloc: ^0.0.2 copied to clipboard
Integration for the Bloc state management library to Bonfire games.
bonfire_bloc 🔥🧱 #
Inspired by flame_bloc, bonfire_bloc offers a simple and natural way (similar to flutter_bloc) to use blocs and
cubits inside a Bonfire game.
Installing #
Simply run:
flutter pub add bonfire_bloc
flutter pub add bonfire
flutter pub add flutter_bloc
Or add custom versions in your pubspec.yaml:
bonfire: ^3...
flutter_bloc: ^8...
How to use #
Lets assume we have a bloc that handles player inventory. First, we need to make it available to our entire game.
We can do that by using BlocProvider or MultiBlocProvider of the flutter_bloc:
return BlocProvider(
create: (context) => InventoryBloc(),
child: BonfireWidget(
...
)
);
Using the bloc at the component level can be done with two approaches:
- Listening to state changes by using
BonfireBlocListenablemixin:
class Player extends SimplePLayer
with BonfireBlocListenable<PlayerInventoryBloc, PlayerInventoryState> {
@override
void onNewState(state) {
updateGear(state);
}
}
- Reading the values by using
BonfireBlocReadermixin:
class Coin extends GameDecoration
with BonfireBlocReader<PlayerInventoryBloc> {
void takeHit() {
bloc.add(const IncrementCoin());
}
}
or with context.read:
class Coin extends GameDecoration{
void takeHit() {
context.read<PlayerInventoryBloc>().add(const IncrementCoin());
}
}
Note that one limitation of the mixin is that it can access only a single bloc.