Flutter 如何正确处理注销

时间:2021-03-10 12:48:35

标签: flutter flutter-bottomnavigation

我有两个注销问题。第一个是当我注销并从登录屏幕单击返回 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,
      ),
    );
  }
}

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

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

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