juan park
juan park

Reputation: 3454

How to route to a different screen after a state change?

I'm using flutter_redux and google_sign_in and I want to route from the Login page to a different page after logging in.

I am handling the API call to Google using middleware that dispatches a LoggedInSuccessfully action. I know I can use Navigator.pushNamed(context, "/routeName") for the actual routing, but I am new to both Flutter and Redux and my problem is that I just don't know where to call this.

The code below is for the GoogleAuthButtonContainer, which is my guess as to where the routing should be. The GoogleAuthButton is just a plain widget with the actual button and layout.

Any help appreciated, thanks!

  @override
  Widget build(BuildContext context) {
    return new StoreConnector<AppState, _ViewModel>(
      converter: _ViewModel.fromStore,
      builder: (BuildContext context, _ViewModel vm) {
        return new GoogleAuthButton(
          buttonText: vm.buttonText,
          onPressedCallback: vm.onPressedCallback,
        );
      },
    );
  }
}

class _ViewModel {
  final String buttonText;
  final Function onPressedCallback;

  _ViewModel({this.onPressedCallback, this.buttonText});

  static _ViewModel fromStore(Store<AppState> store) {
    return new _ViewModel(
        buttonText:
        store.state.currentUser != null ? 'Log Out' : 'Log in with Google',
        onPressedCallback: () {
          if (store.state.currentUser != null) {
            store.dispatch(new LogOut());
          } else {
            store.dispatch(new LogIn());
          }
        });
  }
}

Upvotes: 10

Views: 3258

Answers (1)

Xavi Rigau
Xavi Rigau

Reputation: 1419

You can achieve this by 3 different ways:

  • Call the navigator directly (without using the ViewModel), but this is not a clean solution.
  • Set the navigatorKey and use it in a Middleware as described here
  • And another solution is what I've explained here which is passing the Navigator to the Action class and use it in the Middleware

So to sum up, you probably want to use a Middleware to do the navigation.

Upvotes: 10

Related Questions