简体   繁体   English

如何在 Flutter 加载时导航到另一个页面

[英]How to navigate to another page on load in Flutter

I want to navigate to the login page if there is no logged in user, otherwise display the homepage.如果没有登录用户,我想导航到登录页面,否则显示主页。 I thought of calling Navigator.of(context).push() conditionally inside the build method but that triggers an exception.我想过在 build 方法中有条件地调用Navigator.of(context).push()但这会触发异常。 Is there some method I'm missing that I can override?是否有一些我可以覆盖的方法?

Update to add the Homepage widget更新以添加主页小部件

class HomePage extends StatelessWidget {
final AppUser user;

const HomePage({Key key, this.user}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      title: Text('Rera Farm'),
      actions: <Widget>[
        PopupMenuButton(
          itemBuilder: (BuildContext context) {
            return <PopupMenuEntry>[
              PopupMenuItem(
                child: ListTile(
                  title: Text('Settings'),
                  onTap: () {
                    Navigator.pop(context);
                    Navigator.push(context,
                        MaterialPageRoute(builder: (BuildContext context) 
                      => SettingsPage()
                    ));
                  },
                ),
              ),
            ];
          },
        )
      ],
    ),
    body: _buildBody(context));
}

And the container还有容器

class HomePageContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
 return new StoreConnector<AppState, _ViewModel>(
   converter: _ViewModel.fromStore,
   builder: (BuildContext context, _ViewModel vm) {
     return HomePage(
      user: vm.user,
     );
   },
 );
 }
}

You need to either use a ternary in the onTap if you're using the settings button or, if you just want it to automatically send the user to the correct page when the app starts, you can put the ternary in the MyApp build method.如果您使用设置按钮,则需要在 onTap 中使用三元,或者,如果您只是希望它在应用程序启动时自动将用户发送到正确的页面,您可以将三元放在 MyApp 构建方法中。

If you are using the settings button and just want it to pop back to the previous page if the person is not logged in then you can change NotLoggedIn() to a pop.如果您正在使用设置按钮并且只是希望它在此人未登录的情况下弹回上一页,那么您可以将 NotLoggedIn() 更改为弹出。

For some strange reason SO is refusing to post the code when it is properly formatted with four spaces, exactly as it asks, so I'm just going to make a gist.出于某种奇怪的原因,当代码正确格式化为四个空格时,SO 拒绝发布代码,正如它所要求的那样,所以我只想做一个要点。

https://gist.github.com/ScottS2017/3288c7e7e9a014430e56dd6be4c259ab https://gist.github.com/ScottS2017/3288c7e7e9a014430e56dd6be4c259ab

Here's how I end up doing it.这就是我最终这样做的方式。 I do the checks in the main method, so the user sees the splash screen set in manifest while those weird checks are made:我在main方法中进行检查,因此用户在进行这些奇怪的检查时会看到清单中设置的启动画面:

void main() {
    WidgetsFlutterBinding.ensureInitialized();
    SharedPreferences.getInstance().then((instance) {
        _token = instance.getString("token");
        final _loggedIn = _token != null && token != "";
        runApp(MyApp(loggedIn: _loggedIn));
   });
}

Then in your app add the parameters to switch:然后在您的应用程序中添加要切换的参数:

class MyApp extends StatelessWidget {
final bool loggedIn;

MyApp({this.key, this.loggedIn});

@override
Widget build(BuildContext context) {
    return MaterialApp(
        home: loggedIn ? HomePage() : LoginPage(),
    );
}
}

You can also use Navigator.pushReplacement() if you need to do it below MyApp().如果需要在 MyApp() 下进行,也可以使用Navigator.pushReplacement() ()。 Just posting it here for future generations.只是张贴在这里供后代使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM