首页 > 解决方案 > pushAndRemoveUntil后android的后退按钮不工作

问题描述

我使用 customnavigator 包作为底部导航栏出现在所有页面上。导航键的全局键。我管理所有的事情,比如导航然后弹出页面,但是当我在注销按钮上使用 pushAndRemoveUntill 时出现问题,以便它关闭所有堆栈上的页面正常工作,但是当我移动到注销屏幕时,android 后退按钮停止工作。这是我的底部导航栏页面

  final String userid;
  ProductHome({this.userid});
  @override
  _ProductHomeState createState() => _ProductHomeState();
}

class _ProductHomeState extends State<ProductHome> {

  var _myTabScreens = [
    Chat(),
    WriteMessage(),
    ProductMain(),
    Search(),
    Profile(),
  ];
  @override
  void initState() { 
    super.initState();
     var myFocusNode = FocusNode();
    WidgetsBinding.instance.addPostFrameCallback((_){
      FocusScope.of(context).requestFocus(myFocusNode);
    });
  }
  int _currentIndex=2;
  Widget page=ProductMain();
  GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
    void _selectTab(Widget tabItem, int index) {
    if(page == ProductMain() ){
      navigatorKey.currentState.popUntil((route) => route.isFirst);
    } else {
      setState(() {
        page = _myTabScreens[index];
        _currentIndex = index;
      });
    }
  }
  //form made here
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
            onWillPop: () async {
        final isFirstRouteInCurrentTab =!await navigatorKey.currentState.maybePop();
          if (isFirstRouteInCurrentTab) {
          if (_currentIndex != 2) {
          _selectTab(ProductMain(), 2);
          return false;     
          }
        }
        return isFirstRouteInCurrentTab;
      },
          child: Scaffold(
            floatingActionButton: Padding(
          padding: EdgeInsets.only(top: 31),
          child: SizedBox(
            height: 60,
            width: 70,
            child: FloatingActionButton(
              backgroundColor: Colors.transparent,
              elevation: 0,
              onPressed: () {
                
              },
              child: Container(
                height: 68,
                width: 68,
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.white, width: 4),
                  shape: BoxShape.circle,
                  gradient:_currentIndex==2?LinearGradient(
                    begin: const Alignment(0.7, -0.5),
                    end: const Alignment(0.6, 0.5),
                    colors: [
                      Color(0xFF53a78c),
                      Color(0xFF70d88b),
                    ],
                  ):LinearGradient(
                    begin: const Alignment(0.7, -0.5),
                    end: const Alignment(0.6, 0.5),
                    colors: [
                      Color(0xffDDE2E3),
                      Color(0xffDDE2E3),
                    ],     
                  ),
                ),
                child: IconButton(icon:Icon(Icons.home),onPressed: (){
                  setState(() {
                    navigatorKey.currentState.popUntil((route) => route.isFirst);
                  page=_myTabScreens[2];
                  _currentIndex=2;
                });
                },),
              ),
            ),
          ),
        ),
          body: CustomNavigator(  
      navigatorKey:navigatorKey,
      home: page,
      pageRoute: PageRoutes.materialPageRoute,
          ),
          
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        bottomNavigationBar: BottomAppBar(
        // shape: CircularNotchedRectangle(),
        child: Container(
          color: Colors.white,
          height: 7.8*SizeConfig.heightMultiplier,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              IconButton(
                iconSize: 3*SizeConfig.heightMultiplier,
                padding: EdgeInsets.only(left: 28.0),
                icon: Icon(Icons.message,color:  _currentIndex==0?Colors.green:Color(0xffDDE2E3)),
                onPressed: () {
                  if(_currentIndex==0){
                    navigatorKey.currentState.maybePop();
                  }else{
                    navigatorKey.currentState.popUntil((route) => route.isFirst);
                  }
                  setState(() {
                   page=_myTabScreens[0];
                  _currentIndex=0;
                  });
                },
              ),
              IconButton(
                iconSize: 3*SizeConfig.heightMultiplier,
                padding: EdgeInsets.only(right: 28.0),
                icon: Icon(Icons.edit,color:  _currentIndex==1?Colors.green:Color(0xffDDE2E3)),
                onPressed: () {
                  navigatorKey.currentState.popUntil((route) => route.isFirst);
                  setState(() {
                  page=_myTabScreens[1];
                  _currentIndex=1;
                  });
                },
              ),
              IconButton(
                iconSize: 3*SizeConfig.heightMultiplier,
                padding: EdgeInsets.only(left: 28.0),
                icon: Icon(Icons.search,color:  _currentIndex==3?Colors.green:Color(0xffDDE2E3)),
                onPressed: () {
                  navigatorKey.currentState.popUntil((route) => route.isFirst);
                  setState(() {
                   page=_myTabScreens[3];
                  _currentIndex=3;
                  });
                },
              ),
              IconButton(
                iconSize: 3*SizeConfig.heightMultiplier,
                padding: EdgeInsets.only(right: 28.0),
                icon:Icon(Icons.person,color:  _currentIndex==4?Colors.green:Color(0xffDDE2E3),),
                onPressed: () {
                  navigatorKey.currentState.popUntil((route) => route.isFirst);
                  setState(() {
                    page=_myTabScreens[4];
                  _currentIndex=4;
                  });
                },
              )
            ],
          ),
        ),
        ),
        ),
    );
  }
}```

标签: flutterdart

解决方案


如果您删除,直到,如您所说,所有路线都被清除。因此没有后页可去。


推荐阅读