0 purchases
bonfire bloc
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
copied to clipboard
Or add custom versions in your pubspec.yaml:
bonfire: ^3...
flutter_bloc: ^8...
copied to clipboard
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(
...
)
);
copied to clipboard
Using the bloc at the component level can be done with two approaches:
Listening to state changes by using BonfireBlocListenable mixin:
class Player extends SimplePLayer
with BonfireBlocListenable<PlayerInventoryBloc, PlayerInventoryState> {
@override
void onNewState(state) {
updateGear(state);
}
}
copied to clipboard
Reading the values by using BonfireBlocReader mixin:
class Coin extends GameDecoration
with BonfireBlocReader<PlayerInventoryBloc> {
void takeHit() {
bloc.add(const IncrementCoin());
}
}
copied to clipboard
or with context.read:
class Coin extends GameDecoration{
void takeHit() {
context.read<PlayerInventoryBloc>().add(const IncrementCoin());
}
}
copied to clipboard
Note that one limitation of the mixin is that it can access only a single bloc.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.