bloc listener and multi bloc listener issue

Viewed 677

I need to open bottom navigator sheet once a bloc tells UI to be changed, there is another bloc tells the bottom navigator sheet to be closed. i have implemented it using this code, which is works as needed, using multi bloc listener.

   MultiBlocListener(
                listeners: [
              BlocListener<HomeCubit, HomeState>(
                listener: (context, state) {
                  if (state is OpenBottomSheet) {
                    _scaffoldKey.currentState!
                        .showBottomSheet(
                            (context) => const AddNewNoteBottomSheet())
                        .closed
                        .then(
                          (_) async => context
                              .read<HomeCubit>()
                              .openBottomSheet(isOpenBottomSheet: false),
                        );
                  }
                },
              ),
              BlocListener<NoteFormCubit, NoteFormState>(
                listener: (context, state) {
                  if (state.validToBeSavedToDB) {
                    Navigator.pop(context);
                  }
                },
              ),
            ],
                child: InkWell(
                  onTap: () {
                    if (BlocProvider.of<HomeCubit>(context).state is Init) {
                      return context
                          .read<HomeCubit>()
                          .openBottomSheet(isOpenBottomSheet: true);
                    } else {
                      return context
                          .read<NoteFormCubit>()
                          .addNewNoteButton();
                    }
                  },
                  child: Container(
                    height: 10,
                    width: 20,
                    child: Icon(
                      Icons.add,
                    ),
                  ),
                ))

But my question is Why using only once bloc listener using this way is not implemented well! both blocs are implemented but but only only the first bloc has been change the UI, which open the bottom navigator sheet. the other one which is close the bottom navigator sheet the code is implemented, But does not change the UI.

the code is:

BlocListener<HomeCubit, HomeState>(
              listener: (context, state) {
                 if (state is OpenBottomSheet) {
                        _scaffoldKey.currentState!
                            .showBottomSheet(
                                (context) => const AddNewNoteBottomSheet())
                            .closed
                            .then(
                              (_) async => context
                                  .read<HomeCubit>()
                                  .openBottomSheet(isOpenBottomSheet: false),
                            );
                      }
                     else if (BlocProvider.of<NoteFormCubit>(context)
                        .state
                        .validToBeSavedToDB ==
                    true) {
                  Navigator.pop(context); // Does NOT implement
                }
              },
              child: InkWell(
                onTap: () {
                  if (BlocProvider.of<HomeCubit>(context).state is Init) {
                    return context
                        .read<HomeCubit>()
                        .openBottomSheet(isOpenBottomSheet: true);
                  } else {
                    return context.read<NoteFormCubit>().addNewNoteButton();
                  }
                },
                child: Container(
                  height: 10,
                  width: 20,
                  child: Icon(
                    Icons.add,
                  ),
                ),
              ),)
0 Answers
Related