MultiBlocConsumer constructor

const MultiBlocConsumer({
  1. Key? key,
  2. required List<BlocBase> blocs,
  3. required dynamic listener(
    1. BuildContext,
    2. BlocStates
    ),
  4. required Widget builder(
    1. BuildContext,
    2. BlocStates
    ),
  5. bool buildWhen(
    1. BuildContext,
    2. BlocStates
    )?,
  6. bool listenWhen(
    1. BuildContext,
    2. BlocStates
    )?,
})

MultiBlocConsumer handles building a widget by observing state of variouse Blocs and should be used in combination with the flutter_bloc package. Specify the bloc to observed via the blocs parameter. The builder rebuilds each time a state change occurs and provides a context (BuildContext) and the states (BlocStates). The listener listen each time change occurs and provides a context (BuildContext) and the states (BlocStates).

How to use:

final bloc1 = BlocProvider.of<MyBloc1>(context);
final bloc2 = BlocProvider.of<MyBloc2>(context);
final bloc3 = BlocProvider.of<MyBloc2>(context);

MultiBlocConsumer(
  blocs: [bloc1, bloc2, bloc3],
  listener: (context, states) {
    final state1 = states.get<MyBloc1State>();
    final state2 = states.get<MyBloc2State>();
    final state3 = states.get<MyBloc3State>();

    if (state1 is Loading || state2 is Loading || state3 is Loading) {
       ... Do something
    } else {
      ... Do something
    }
  }
  builder: (context, states) {
    final state1 = states.get<MyBloc1State>();
    final state2 = states.get<MyBloc2State>();
    final state3 = states.get<MyBloc3State>();

    if (state1 is Loading || state2 is Loading || state3 is Loading) {
      return Text("Loading");
    } else {
      return Text("SHow some content");
    }
  }
);

Implementation

const MultiBlocConsumer({super.key,
  required List<BlocBase> blocs,
  required Function(BuildContext, BlocStates) listener,
  required Widget Function(BuildContext, BlocStates) builder,
  bool Function(BuildContext, BlocStates)? buildWhen,
  bool Function(BuildContext, BlocStates)? listenWhen

}) :
      _blocs = blocs,
      _listener = listener,
      _builder = builder,
      _buildWhen = buildWhen,
      _listenWhen = listenWhen;