状态更改后如何路由到不同的屏幕?

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

我正在使用 flutter_redux 和 google_sign_in,我想在登录后从登录页面转到另一个页面。

我正在使用调度 LoggedInSuccessfully 操作的中间件处理对 Google 的 API 调用。我知道我可以使用 Navigator.pushNamed(context, "/routeName") 进行实际路由,但我是 Flutter 和 Redux 的新手,我的问题是我只是不知道 在哪里 调用它。

下面的代码是针对 GoogleAuthButtonContainer 的,这是我对路由位置的猜测。 GoogleAuthButton 只是一个带有实际按钮和布局的普通小部件。

感谢任何帮助,谢谢!

  @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());
          }
        });
  }
}

您可以通过 3 种不同的方式实现此目的:

  • 直接调用导航器(不使用 ViewModel),但这不是一个干净的解决方案。
  • 设置 navigatorKey 并按照 here
  • 所述在 Middleware 中使用它
  • 另一种解决方案是我已经解释过的 here,它将 Navigator 传递给 Action class 并在 Middleware 中使用它

总而言之,您可能想使用 Middleware 来进行导航。