0
votes

i'm trying to switch screens (stateless widgets) using bloc, when doing so using the BlocBuilder to output different widgets according the state, the context is changing to the new widget that is displayed. the state listener is called in the new widget.

i managed to show a widget when the state changed, the problem is when the state changes again, then the context is changed and the builder function is called in the widget that was shown.

so i created an inheritedWidget that will contain the bloc, so i could dispatch an event from the subwidget to happen on the parent and then show a different widget based on state.

class SignupViewManager extends InheritedWidget {
  final SignupBloc bloc;
  final Widget child;

  SignupViewManager({Key key, this.child, this.bloc}) : super(key: key, child: child);

  static SignupViewManager of(BuildContext context) {
    return (context.inheritFromWidgetOfExactType(SignupViewManager) as SignupViewManager);
  }

  @override
  bool updateShouldNotify(SignupViewManager oldWidget) {
    return true;
  }
}

class SignupHomePage extends StatelessWidget {
  final SignupBloc signupBloc = SignupBloc();

  SignupHomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SignupViewManager(
        bloc: SignupBloc(),
        child: BlocProvider<SignupBloc>(
          builder: (context) => signupBloc,
          child: BlocBuilder<SignupBloc, SignupState>(
              builder: (context, state) => _signupBuilder(context, state),
              condition: (previousState, state) => _signupCondition(previousState, state),
        ),
      )
    );
  }

  Widget _signupBuilder(BuildContext context, SignupState state) {
    print("IN SIGNUP MAIN BUILDER $state");
    // Changing the UI based on the current state
    if (state is SignupInitial) { // Show credentials for initial state
      return SignupCredentials();
    } else if (state is SavedCredentials) { // Show Agreements after credentials has been saved.
      return SignupAgreements(user: state.user);
    }

    return Text("default");
  }
}

class SignupCredentials extends StatelessWidget {
  SignupBloc signupBloc;

  void _signup(email, password, confirmPassword) {
    if(password == confirmPassword) {
      signupBloc.add(SaveCredentials(email, password));
    } else {
      print("passwords do not match");
    }
  }

  Widget _bloc() {
    return BlocBuilder<SignupBloc, SignupState>(
        builder: (context, state) {
            print("IN SIGNUP credentials builder $state");

            if(state is SavedCredentials) {
            return SignupDetails();
            }

            return Text("${state.runtimeType}");
        },
    );
  }
}

So when the state is set to SignupInitial, the SignupCredentials stateless widget should appear. and then when the user enters the credentials and submits them, an event of SavedCredentials is dispatched, and the SignupAgreements should appear. but what happens is that the SignupCredentials builder is receiving the new state instead of SignupHomePage.

1

1 Answers

0
votes

I solved this.

I changed the bloc provider to be of type SignupBloc.

@override
  Widget build(BuildContext context) {
    return AuthenticationPageLayout(
      color: Colors.white,
      opacity: 0.05,
      child: SignupViewManager(
        child: BlocProvider<SignupBloc>(
            builder: (context) => SignupBloc(),
            child: BlocListener<SignupBloc, SignupState>(
                listener: (context, state) => _signupListener(context, state),
                condition: (previousState, state) => _signupCondition(previousState, state),
                child: BlocBuilder<SignupBloc, SignupState>(
                builder: (context, state) => _signupBuilder(context, state),
                condition: (previousState, state) => _signupCondition(previousState, state),
                )
            )
        );
      )
    );
  }

then i refered the bloc provider using this line

BlocProvider.of<SignupBloc>(context)