Flutter Navigator acting weird

Viewed 43

I am trying to implement navigator 2.0 using flutter bloc but the problem is when I add new event and update state it does not rebuild even tho the bloc builder is working but when I hot reload all the previous pages in the list stack up and I can press back they all pop and when i reach at the first page it again stop rebuilding. I am posting a some chunk of code.

Here is the delegate class

  class MyRouterDelegate extends RouterDelegate
    with ChangeNotifier, PopNavigatorRouterDelegateMixin {
  bool showOtherPage = false;

  @override
  final GlobalKey<NavigatorState> navigatorKey;

  MyRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>();

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthenticationBloc, AuthenticationState>(
      builder: (context, authenticationState) {
        print(authenticationState.pages);
        return Navigator(
          key: navigatorKey,
          pages: authenticationState.pages,
          onPopPage: (route, result) {
            if (!route.didPop(result)) return false;

            BlocProvider.of<AuthenticationBloc>(context).add(UserLogoutEvent());
            return true;
          },
        );
      },
    );
  }

  @override
  Future<void> setNewRoutePath(configuration) async => null;
}

Here is my bloc

class AuthenticationBloc
    extends Bloc<AuthenticationEvent, AuthenticationState> {
  AuthenticationBloc()
      : super(AuthenticationState(pages: [
          MaterialPage(child: MyConnexionWidget(), key: ValueKey('my page'))
        ]));

  @override
  Stream<AuthenticationState> mapEventToState(
      AuthenticationEvent event) async* {
    switch (event.runtimeType) {
      case UserLoginEvent:
        final oldState = state.pages;
        oldState
            .add(MaterialPage(child: MyHomeWidget(), key: ValueKey('home')));
        yield AuthenticationState(pages: oldState);
        break;
      case UserLogoutEvent:
        final oldState = state.pages;
        oldState.removeLast();
        yield AuthenticationState(pages: oldState);
        break;
    }
  }
}

// Authentication events
abstract class AuthenticationEvent {}

class UserLogoutEvent extends AuthenticationEvent {}

class UserLoginEvent extends AuthenticationEvent {}

// Authentication states
class AuthenticationState {
  final List<Page> pages;

  const AuthenticationState({required this.pages});
}

First page

class MyConnexionWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Navigator 2.0 101 - Connexion screen'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FlatButton(
              child: Container(
                padding: EdgeInsets.all(8.0),
                color: Colors.greenAccent,
                child: Text('Click me to connect.'),
              ),
              onPressed: () => BlocProvider.of<AuthenticationBloc>(context)
                  .add(UserLoginEvent()),
            )
          ],
        ),
      ),
    );
  }
}
0 Answers
Related