【问题标题】:Flutter: conditional bottom navigation bar, to show pages based on if conditionFlutter:有条件的底部导航栏,根据if条件显示页面
【发布时间】:2020-04-15 08:33:15
【问题描述】:

我已经使用下面列出的逻辑实现了一个底部导航栏,我想检查用户是否已登录。如果用户在第三个选项卡上登录,我想显示个人资料页面而不是登录页面。这是我显示底部导航栏的代码,我被卡在 checkIsLoggedIn() 异步函数之后要做什么。

之后我构建小部件列表以在 3 个选项卡中显示页面。 代码如下,

所以代替 SignIn(),我需要显示,如果用户已登录,则显示登录,否则显示个人资料页面。 请帮帮我。

【问题讨论】:

    标签: flutter conditional-statements flutter-layout


    【解决方案1】:

    这可能会有所帮助。

    Future<void> checkIfLoggedIn() async {
      SharedPreferences localStorage = await SharedPreferences.getInstance();
      var id = localStorage.getString('id');
      if (id != null) {
        setState(() {
          _children[2] = ProfilePage();
        });
      }
    }
    

    【讨论】:

      【解决方案2】:


      checkIfLoggedIn() 内,连同_isLoggedIn 一起编写以下代码:
      _children[2] = Profile()

      【讨论】:

        【解决方案3】:

        你可以设置最后一个BottomNavigationItem如下

        BottomNavigationItem(
            icon:_isLoggedIn ? Icons.person : Icons.key,
            title:Text(_isLoggedIn ? 'Account' : 'Log in' )
        )
        

        您可以使用_isLoggedIn 变量通过相同的三元运算符处理点击事件并在build() 中设置正确的页面

        build(){
            return Scaffold(
                body:_isLoggedIn?ProfilePage():LoginPage()
            );
        }
        

        【讨论】:

        • 但对于正文:我使用的是小部件列表_children[_currentIndex],它不仅仅是一页。
        • 我不知道整个代码..我提供参考...您可以按照您的风格实现...
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-15
        • 2022-11-08
        • 2023-02-26
        • 2020-11-27
        • 1970-01-01
        • 2021-06-04
        • 1970-01-01
        相关资源
        最近更新 更多