felangel / bloc

A predictable state management library that helps implement the BLoC design pattern

Home Page:https://bloclibrary.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Correct way to handle nested login screen and conditionally render different screens.

ignicubeLaeeq opened this issue · comments

I am using bloc to conditionally render various screens as below.

Expected behavior is that when i log in, user / doctor screen is rendered. Whereas, after login, the auth screen is not replaced with new screen. Instead new screen is rendered below auth screen and auth screen needs to be popped from context.

P.S In my auth flow i am not at the top of navigator stack. And if i am at the top of the stack there is no such issue.

Any help understanding why this is happening and how to handle it better.

class AuthCheck extends StatelessWidget {
  const AuthCheck({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthBloc, AuthState>(
        builder: (context, state) {
          final user = state.user;
          final loading = state.loading;
          final accountType = state.accountType;
          if (user == null) {
            return loading ? const SplashScreen() : const AuthScreen();
          } else {
            switch (accountType) {
              case AccountType.doctor:
                'Doctor should render'.log();
                return const DoctorApp();
              case AccountType.user:
                'User should render'.log();
                return const UserApp();
              case AccountType.admin:
                return const AdminApp();
              default:
                return const UserApp();
            }
          }
        },
    );
  }

It is not clear, what the issue is. What do you mean by below auth screen and popped from context (in your example is no page navigation)?

Please surround code blocks with ```.