【问题标题】:Flutter How To Properly Handle LogoutFlutter 如何正确处理注销
【发布时间】:2021-03-10 12:48:35
【问题描述】:

我有两个注销问题。第一个是当我注销并从登录屏幕单击返回 btn 时,它会再次返回主屏幕。我正在使用Navigator.of(context).pushAndRemoveUntil(),但仍然是这个问题。第二个问题是,即使在我注销后,底部导航栏仍然附着在登录屏幕上。

在我对底部导航进行更改之前,我没有遇到这些问题。我进行了更改,以便在我在主屏幕、个人资料屏幕中打开另一个屏幕时底部导航应该保持连接状态。

这是我的底部导航栏逻辑:

String _currentPage = "Page1";
  List<String> pageKeys = ["Page1", "Page2", "Page3"];
  Map<String, GlobalKey<NavigatorState>> _navigatorKeys = {
    "Page1": GlobalKey<NavigatorState>(),
    "Page2": GlobalKey<NavigatorState>(),
    "Page3": GlobalKey<NavigatorState>(),
  };

  int _selectedIndex = 0;

  void _selectTab(String tabItem, int index) {
    if (tabItem == _currentPage) {
      _navigatorKeys[tabItem].currentState.popUntil((route) => route.isFirst);
    } else {
      setState(() {
        _currentPage = pageKeys[index];
        _selectedIndex = index;
      });
    }
  }

@override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    final kConstant = 0.099;

    final String currentUserId = Provider.of<UserData>(context).currentUserId;

    return WillPopScope(
      onWillPop: () async {
        final isFirstRouteInCurrentTab =
            !await _navigatorKeys[_currentPage].currentState.maybePop();
        if (isFirstRouteInCurrentTab) {
          if (_currentPage != "Page1") {
            setState(() {
              _currentPage = pageKeys[0];
              _selectedIndex = 0;
            });
            return false;
          }
        }
        return isFirstRouteInCurrentTab;
      },
      child: Scaffold(
          body: Stack(
            children: <Widget>[
              _buildOffstageNavigator("Page1"),
              _buildOffstageNavigator("Page2"),
              _buildOffstageNavigator("Page3"),
            ],
          ),
          bottomNavigationBar: SizedBox(
            height: size.height * kConstant,
            child: BottomNavigationBar(
              onTap: (int index) {
                _selectTab(pageKeys[index], index);
              },
              elevation: 4,
              type: BottomNavigationBarType.fixed,
              currentIndex: _selectedIndex,
              showUnselectedLabels: true,
              items: [
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  label: 'Home',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.add),
                  label: 'Add',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.perm_identity),
                  label: 'Profile',
                ),
              ],
            ),
          )),
    );
  }

  Widget _buildOffstageNavigator(String tabItem) {
    return Offstage(
      offstage: _currentPage != tabItem,
      child: TabNavigator(
        navigatorKey: _navigatorKeys[tabItem],
        tabItem: tabItem,
      ),
    );
  }
}

请帮我解决这个问题。

【问题讨论】:

    标签: flutter flutter-bottomnavigation


    【解决方案1】:

    你会尝试用下面的代码再做一次吗?

    Navigator.of(context).popUntil((route) => route.isFirst);
    

    【讨论】:

    • 谢谢。我尝试在注销按钮上使用Navigator.of(context, rootNavigator: true).pushReplacement(),它按预期工作。
    【解决方案2】:

    问题与导航器有关。如果您有嵌套的导航器,则 pushAndRemoveUntil 不会完成这项工作,因为它不会将注销屏幕推送到路线或最顶部的导航器上,而是推送到子导航器上,例如屏幕。因此,您必须在导航到注销屏幕时明确传递路线导航器,例如

    Navigator.of(context, rootNavigator: true).pushReplacement(                          
    MaterialPageRoute(builder: (context) => LogoutPage()));
    

    这将弹出任何子导航器并转到顶部屏幕。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-31
      • 2010-09-22
      • 2018-01-15
      • 2019-01-19
      • 2020-06-27
      • 2013-09-27
      • 2018-08-04
      • 1970-01-01
      相关资源
      最近更新 更多