Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

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());
          }
        });
  }
}
like image 200
TeabaggingSanta Avatar asked Mar 19 '18 22:03

TeabaggingSanta


People also ask

What is navigate replace?

option - replace Normally a call to navigate will push a new entry into the history stack so the user can click the back button to get back to the page. If you pass replace: true to navigate then the current entry in the history stack will be replaced with the new one.

How do I go back in react navigation?

To go back to the previous page with React router: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Call the navigate() function passing it -1 - navigate(-1) . Calling navigate with -1 is the same as hitting the back button.


1 Answers

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.

like image 165
Xavi Rigau Avatar answered Sep 21 '22 02:09

Xavi Rigau