mono_bloc_flutter 1.0.0
mono_bloc_flutter: ^1.0.0 copied to clipboard
Flutter widgets and utilities for mono_bloc. Provides MonoBlocActionListener for handling side effects in Flutter apps.
mono_bloc_flutter #
Flutter widgets and utilities for MonoBloc. Provides MonoBlocActionListener for handling side effects in Flutter apps.
Installation #
dependencies:
mono_bloc_flutter: ^1.0.0
What's Included #
This package exports everything you need for using MonoBloc in Flutter:
MonoBlocActionListener- Widget for listening to bloc actions- All of
flutter_bloc- BlocProvider, BlocBuilder, BlocListener, etc. - All of
mono_bloc- Annotations, base classes, and core functionality
You only need one import:
import 'package:mono_bloc_flutter/mono_bloc_flutter.dart';
Usage #
Define your bloc with actions #
part 'cart_bloc.g.dart';
@MonoBloc()
abstract class CartBloc extends _$CartBloc<CartState> {
CartBloc._() : super(const CartState());
factory CartBloc() = _$CartBlocImpl;
@event
CartState _onCheckout() {
if (state.items.isNotEmpty) {
navigateToCheckout();
}
return state.copyWith(items: []);
}
@action
void navigateToCheckout();
}
Listen to actions in your widget #
Option 1: Implement the actions interface
class CartPage extends StatelessWidget implements CartBlocActions {
const CartPage({super.key});
@override
Widget build(BuildContext context) {
return CartBlocActionListener(
actions: CartBlocActions.of(this),
child: CartView(),
);
}
@override
void navigateToCheckout(BuildContext context) {
Navigator.pushNamed(context, '/checkout');
}
}
Option 2: Use inline actions with .when()
class CartPage extends StatelessWidget {
const CartPage({super.key});
@override
Widget build(BuildContext context) {
return CartBlocActionListener(
actions: CartBlocActions.when(
navigateToCheckout: (context) {
Navigator.pushNamed(context, '/checkout');
},
),
child: CartView(),
);
}
}
Features #
MonoBlocActionListener #
A Flutter widget that listens to actions from a MonoBloc and automatically provides BuildContext to action handlers.
Key benefits:
- ✅ Automatic subscription management (subscribe/unsubscribe)
- ✅ BuildContext automatically provided to all action handlers
- ✅ Type-safe action handling with generated interfaces
- ✅ Two usage patterns: interface implementation or inline callbacks
- ✅ Integrates seamlessly with BlocProvider
FlutterMonoBlocActions #
Base class for generated action handlers in Flutter. Automatically included in generated code when using @action annotations.
Documentation #
For complete documentation, examples, and guides:
- MonoBloc package - Core functionality and annotations
- MonoBloc hooks - Flutter Hooks integration
License #
MIT License - see the LICENSE file for details.