首页 文章

如何在 flutter 中处理不同的登录导航流程?

提问于
浏览
5

我在登录时忘记设置导航(忘记密码,signup/login)与登录时相比(家庭,注销,很多东西)。

我完全不知道如何做到这一点。我看到的所有建议都退出了系统的一部分,一个页面显示登录,但这在这里不起作用。如果我共享导航,那么应用程序其余部分的每个页面都需要登录检查,这听起来有点恼火。是否有一种简单的方法来换出导航设置?可以根据用户登录的 in/out 状态动态添加导航?

我可以将导航类本身子类化并以这种方式处理它吗?

在本机中,您可以通过更换登录的导航器和登录的导航器之间的导航器来实现此目的。寻找与此类似的结果。

2 回答

  • 2

    React-Native 允许嵌套导航器,但颤动不会。虽然没有嵌套任何导航器,但有多种方法可以做到这一点,下面显示了如何使用颤振来完成它的简单示例。

    例:

    import 'package:flutter/material.dart';
    
    void main() => runApp(new MyApp());
    
    // Main Application
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Example',
          // Routes
          routes: <String, WidgetBuilder>{
            '/': (_) => new Login(), // Login Page
            '/home': (_) => new Home(), // Home Page
            '/signUp': (_) => new SignUp(), // The SignUp page
            '/forgotPassword': (_) => new ForgotPwd(), // Forgot Password Page
            '/screen1':(_) => new Screen1(), // Any View to be navigated from home
          },
        );
      }
    }
    
    // The login page 
    class Login extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Text("Login Page"),
    
                // The button on pressed, logs-in the user to and shows Home Page
                new FlatButton(
                    onPressed: () =>
                        Navigator.of(context).pushReplacementNamed("/home"),
                    child: new Text("Login")),
    
                // Takes user to sign up page
                new FlatButton(
                    onPressed: () => Navigator.of(context).pushNamed("/signUp"),
                    child: new Text("SignUp")),
    
                // Takes user to forgot password page
                new FlatButton(
                    onPressed: () =>
                        Navigator.of(context).pushNamed("/forgotPassword"),
                    child: new Text("Forgot Password")),
              ],
            ),
          ),
        );
      }
    }
    
    // Home page
    class Home extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Text("Home Page"),
    
                // Logs out user instantly from home
                new FlatButton(
                    onPressed: () => Navigator.of(context).pushReplacementNamed("/"),
                    child: new Text("Logout")),
    
                // Takes user to Screen1
                new FlatButton(
                    onPressed: () => Navigator.of(context).pushNamed("/screen1"),
                    child: new Text("Screen 1")),
              ],
            ),
          ),
        );
      }
    }
    
    // Sign Up Page
    class SignUp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Text("Sign Up Page"),
    
                // To make an api call with SignUp data and take back user to Login Page
                new FlatButton(
                    onPressed: () {
                      //api call to sign up the user or whatever
                      Navigator.of(context).pop();
                    },
                    child: new Text("SignUp")),
              ],
            ),
          ),
        );
      }
    }
    
    // Forgot Password page
    class ForgotPwd extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Text("Sign Up"),
    
                // Make api call to resend password and take user back to Login Page
                new FlatButton(
                    onPressed: () {
                      //api call to reset password or whatever
                      Navigator.of(context).pop();
                    },
                    child: new Text("Resend Passcode")),
              ],
            ),
          ),
        );
      }
    }
    
    // Any Screen     
    class Screen1 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Text("Screen 1"),
    
                // Takes the user to the view from which the user had navigated to this view
                new FlatButton(
                    onPressed: () => Navigator.of(context).pop(),
                    child: new Text("Back")),
    
                // Takes back the user to Home page and Logs out the user
                new FlatButton(
                    onPressed: () async {
                      Navigator.of(context).popUntil(ModalRoute.withName("/home")); // Use popUntill if you want to reset all routes untill now and completely logout user
                      Navigator.of(context).pushReplacementNamed("/");
                      // Just to show login page and resume back after login
                      // Navigator.of(context).pushNamed('/Login');
                      // On login page after successful login Navigator.of(context).pop();
                      // the app will resume with its last route.
                    },
                    child: new Text("Logout")),
              ],
            ),
          ),
        );
      }
    }
    

    注意:我不是说这是最好的方法,但是示例显示了一种简单的方法。

    希望有所帮助!

  • 0

    你可以这样做

    import 'package:app/pages/home.page.dart';
    import 'package:app/pages/login.page.dart';
    import 'package:app/services/auth.service.dart';
    import 'package:flutter/material.dart';
    
    AuthService appAuth = new AuthService();
    
    void main() async {
      // Set default home.
      Widget _defaultHome = new LoginPage();
    
      // Get result of the login function.
      bool _result = await appAuth.login();
      if (_result) {
        _defaultHome = new HomePage();
      }
    
      // Run app!
      runApp(new MaterialApp(
        title: 'App',
        home: _defaultHome,
        routes: <String, WidgetBuilder>{
          // Set routes for using the Navigator.
          '/home': (BuildContext context) => new HomePage(),
          '/login': (BuildContext context) => new LoginPage()
        },
      ));
    }
    

    详细解释这里https://medium.com/@anilcan/how-to-use-dynamic-home-page-in-flutter-83080da07012

相关问题