mono_bloc_flutter 1.0.0 copy "mono_bloc_flutter: ^1.0.0" to clipboard
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.

example/example.dart

/// Example of using mono_bloc_flutter widgets.
///
/// This package provides Flutter widgets for handling MonoBloc actions,
/// which are side effects like navigation, dialogs, and snackbars.
///
/// ## Setup
///
/// Add dependencies to `pubspec.yaml`:
/// ```yaml
/// dependencies:
///   flutter_bloc: ^9.0.0
///   mono_bloc: ^1.0.0
///   mono_bloc_flutter: ^1.0.0
///
/// dev_dependencies:
///   build_runner: ^2.4.0
///   mono_bloc_generator: ^1.0.0
/// ```
///
/// ## Defining a Bloc with Actions
///
/// ```dart
/// import 'package:mono_bloc/mono_bloc.dart';
///
/// part 'cart_bloc.g.dart';
///
/// @MonoBloc()
/// abstract class CartBloc extends _$CartBloc<CartState> {
///   CartBloc._() : super(const CartState());
///
///   factory CartBloc() = _$CartBlocImpl;
///
///   @event
///   CartState _onAddItem(Product product) {
///     showNotification(message: 'Added ${product.name}');
///     return state.copyWith(items: [...state.items, product]);
///   }
///
///   @event
///   Future<CartState> _onCheckout() async {
///     await processPayment();
///     navigateToConfirmation();
///     return state.copyWith(items: []);
///   }
///
///   // Actions - side effects that don't modify state
///   @action
///   void showNotification({required String message});
///
///   @action
///   void navigateToConfirmation();
/// }
/// ```
///
/// ## Using MonoBlocActionListener
///
/// The generator creates a convenient typedef `CartBlocActionListener`
/// and a helper class `CartBlocActions` for handling actions:
///
/// ```dart
/// import 'package:flutter/material.dart';
/// import 'package:flutter_bloc/flutter_bloc.dart';
/// import 'package:mono_bloc_flutter/mono_bloc_flutter.dart';
///
/// class CartPage extends StatelessWidget {
///   @override
///   Widget build(BuildContext context) {
///     return BlocProvider(
///       create: (_) => CartBloc(),
///       child: CartBlocActionListener(
///         actions: CartBlocActions.when(
///           showNotification: (context, {required message}) {
///             ScaffoldMessenger.of(context).showSnackBar(
///               SnackBar(content: Text(message)),
///             );
///           },
///           navigateToConfirmation: (context) {
///             Navigator.pushNamed(context, '/confirmation');
///           },
///         ),
///         child: CartView(),
///       ),
///     );
///   }
/// }
/// ```
///
/// ## Key Features
///
/// - **Context-aware actions**: All action handlers receive `BuildContext`
/// - **Type-safe**: Generated code ensures all actions are handled
/// - **Automatic subscription**: Widget manages action stream lifecycle
/// - **Bloc lookup**: Automatically finds bloc from widget tree if not provided
library;

import 'package:flutter/material.dart';

void main() {
  runApp(const ExampleApp());
}

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('See documentation for mono_bloc_flutter usage'),
        ),
      ),
    );
  }
}
0
likes
0
points
72
downloads

Publisher

verified publisherwestito.dev

Weekly Downloads

Flutter widgets and utilities for mono_bloc. Provides MonoBlocActionListener for handling side effects in Flutter apps.

Repository (GitHub)
View/report issues

Topics

#bloc #flutter #state-management

License

unknown (license)

Dependencies

flutter, flutter_bloc, meta, mono_bloc

More

Packages that depend on mono_bloc_flutter