mono_bloc_hooks 1.0.0 copy "mono_bloc_hooks: ^1.0.0" to clipboard
mono_bloc_hooks: ^1.0.0 copied to clipboard

Flutter hooks for MonoBloc - easily handle actions and state in HookWidget components

mono_bloc_hooks #

pub version Tests

Flutter hooks integration for MonoBloc actions.

Installation #

dependencies:
  mono_bloc_flutter: ^1.0.0
  mono_bloc_hooks: ^1.0.0
  flutter_hooks: ^0.21.0  # Required peer dependency

Usage #

Define your MonoBloc with actions #

import 'package:mono_bloc_flutter/mono_bloc_flutter.dart';

part 'counter_bloc.g.dart';

@MonoBloc()
abstract class CounterBloc extends _$CounterBloc<int> {
  CounterBloc._() : super(0);
  factory CounterBloc() = _$CounterBlocImpl;

  @event
  int _onIncrement() {
    final newValue = state + 1;
    if (newValue >= 10) {
      showMessage('Maximum reached!');
    }
    return newValue;
  }

  @action
  void showMessage(String message);
}

Use in HookWidget #

Option 1: Using .when() for inline actions

import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:mono_bloc_hooks/mono_bloc_hooks.dart';

class CounterPage extends HookWidget {
  const CounterPage({super.key});

  @override
  Widget build(BuildContext context) {
    final bloc = context.read<CounterBloc>();
    
    // Automatically handles subscription lifecycle
    useMonoBlocActionListener(
      bloc,
      CounterBlocActions.when(
        showMessage: (context, message) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text(message)),
          );
        },
      ),
    );

    return BlocBuilder<CounterBloc, int>(
      builder: (context, count) => Text('$count'),
    );
  }
}

Option 2: Using .of() with inline implementation

Useful when you want organized, type-safe action handling:

import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:mono_bloc_hooks/mono_bloc_hooks.dart';

class CounterPage extends HookWidget implements CounterBlocActions {
  const CounterPage({super.key});

  @override
  Widget build(BuildContext context) {
    final bloc = context.read<CounterBloc>();
    
    // Use .of() to pass this widget as action handler
    useMonoBlocActionListener(
      bloc,
      CounterBlocActions.of(this),
    );

    return BlocBuilder<CounterBloc, int>(
      builder: (context, count) => Text('$count'),
    );
  }

  // Implement action handlers
  @override
  void showMessage(BuildContext context, String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(message),
        backgroundColor: Colors.orange,
      ),
    );
  }
}

Documentation #

For complete documentation, examples, and actions guide, see the MonoBloc package.

License #

MIT License - see the LICENSE file for details.

0
likes
0
points
74
downloads

Publisher

verified publisherwestito.dev

Weekly Downloads

Flutter hooks for MonoBloc - easily handle actions and state in HookWidget components

Repository (GitHub)
View/report issues

Topics

#bloc #flutter #hooks #state-management

License

unknown (license)

Dependencies

bloc, flutter, flutter_bloc, flutter_hooks, meta, mono_bloc

More

Packages that depend on mono_bloc_hooks