Bloc: Navigation using BlocListener

Created on 7 Jun 2019  路  4Comments  路  Source: felangel/bloc

Hello

We are using BlocListener and following this:
https://felangel.github.io/bloc/#/recipesflutternavigation

We are able to navigate from screen 1 to screen 2. However, navigate back (using back button) doesn't work

Posting the code below


BlocListener(
      bloc: _dBloc,
      listener: (BuildContext context, DState state) {
        if (state is IndividualState) {

          Navigator.of(context).push(
            PageRouteBuilder(
              pageBuilder: (_, __, ___) => Screen2(this_d),
              transitionsBuilder:
                  (context, animation, secondaryAnimation, child) =>
                  FadeTransition(opacity: animation, child: child),
            ),
          );
          //return LoadingIndicator();

        }
      },
      child: BlocProvider<DBloc>(
      bloc: _dBloc,

      child: MaterialApp(
        home: BlocBuilder<DEvent, DState>(
          bloc: _dBloc,
          builder: (BuildContext context, DState state) {
            print("State in Screen1 " + state.toString());
            if (state is IndividualState) {
              return Screen1();
            }
            if (state is ListAllState) {
              _results = state.dRepository.results;
              print("Result in HomeScreen " +
                  _results.elementAt(0).goals.toString());
              return SafeArea(
                child: Scaffold(

                  title: Text("Screen1"),
                  body: Column(
                    children: <Widget>[
                      ...
                      ),
                    ],
                  ),
                ),
              );
            }
            if (state is PersonalListingState) {
              return LoadingIndicator();
            }
          },
        ),
      ),
    ),
    );

What is the right way to do this while using bloc?

Thanks,
Chethan

question

Most helpful comment

@felangel bro..thanks for pointing that out. working fine now :)

All 4 comments

Hi @chethanprabhakar 馃憢
Thanks for opening an issue!

Regarding your question, can you confirm that this is a bloc issue? Does the issue only occur if you navigate via BlocListener?

You might be missing a Scaffold in Screen2. Can you provide the code for your Screen2 widget?

Thanks! 馃憤

Hi @felangel

Thanks for your reply

We are able to navigate from screen 1 to screen 2. The issue is with the back button in Screen 2 ever since the bloc pattern was implemented.

Posting Screen 2 code below

BlocListener(
      bloc: _dBloc,
      listener: (BuildContext context, DState state) {
        if (state is GIndividualState) {

          Navigator.of(context).push(
            PageRouteBuilder(
              pageBuilder: (_, __, ___) => Screen3(),
              transitionsBuilder:
                  (context, animation, secondaryAnimation, child) =>
                      FadeTransition(opacity: animation, child: child),
            ),
          );
        }
      },
      child: BlocProvider<DBloc>(
        bloc: _dBloc,
        child: MaterialApp(
          home: BlocBuilder<DEvent, DState>(
            bloc: _dBloc,
            builder: (BuildContext context, DState state) {
              print("State in Screen 2 " + state.toString());

              if (state is NewAddState) {


                return Screen3();
              }
              if (state is GIndividualState) {
                return Screen2();
              }
              if (state is PersonalListingState) {
                return SafeArea(
                  child: Scaffold(

                    drawer: CustomDrawer(),
                    body: CustomScrollView(
                      slivers: <Widget>[

                        ...
                   ],
                    ),
                    bottomNavigationBar: CustomNavBar(), ** > //it has a back button **
                  ),
                );
              }
            },
          ),
        ),
      ),
    );
  }

Thanks
Chethan

Why do you have multiple MaterialApp widgets? I notice you have a MaterialApp widget per screen which might be causing these problems. You should only have a single MaterialApp per application. If that doesn鈥檛 help would you be able to send me a link to the code so that I can debug it locally? Thanks!

@felangel bro..thanks for pointing that out. working fine now :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tigranhov picture tigranhov  路  3Comments

Reidond picture Reidond  路  3Comments

timtraversy picture timtraversy  路  3Comments

clicksocial picture clicksocial  路  3Comments

nerder picture nerder  路  3Comments