Flutter

Flutter Navigation路由跳转/回退

2023-06-29  本文已影响0人  xieyinghao

1、切换根控制器
如果当前栈已经销毁了 就不能在返回了 否则直接就是黑屏

//前进
  Navigator.of(context)
            .pushNamedAndRemoveUntil(
            “跳转路径”,
            ModalRoute.withName('/demo'),//清除旧栈需要保留的栈 不清除就不写这句
            arguments:{"data":“233”}//传值
        );
 
 
Navigator.pushNamedAndRemoveUntil(
      context,
     "跳转路径", (route) => false,//true保留跳转的当前栈   false 不保留
    ); 

例子:

Navigator.of(context).pushNamedAndRemoveUntil("/registerpage", (Route route) => false);

2、跳转 push

    Navigator.push(context,
        MaterialPageRoute(builder: (BuildContext context) {
          return PersonSystemSetting(logoutClick: _logout,);
        }));

详细:

Push跳转:

Navigator.push(

                context,

                MaterialPageRoute(

                    //fullscreenDialog: true, //从底部弹出

                    builder: (BuildContext context) {

                      return Login();

                    }),

              );

或 (等价于):

Navigator.of(context)

                  .push(MaterialPageRoute(builder: (BuildContext context) {

                return Login();

              }));

或:

Navigator.pushNamed(context, '/Login').then((value){

      });



跳转并销毁之前的页面,返回到指定页面:

//跳转顺序是Screen1—>Screen2—>Screen3—>Screen4

当从Screen3跳转到Screen4的时候我们希望将Screen2,Screen3从栈里面移除掉,这样在Screen4点击返回就能直接回到Screen1

Navigator.of(context).pushAndRemoveUntil(

MaterialPageRoute(builder: (context) => Screen4()),

ModalRoute.withName('/screen1'));



//打开Screen4页面,并销毁当前页:Screen2—>Screen3—>Screen4

Navigator.of(context).pushReplacementNamed('/screen3');

//也可以使用以下方式

Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context)=>Screen3()));



Pop退出:

Navigator.pop(context);

//或:

Navigator.of(context).pop();

//或:带参数返回,push接收:.then((value) {print('------回传参数:$value');})

Navigator.pop(context, 'YES');

Navigator.pop(context, {

   'name': '回传参数'

                });



一直退出直到某一个页面:

//当前在Screen4页面,点击回到Screen1,连带着Screen2,Screen3也一起退出

Navigator.of(context).popUntil(ModalRoute.withName('/screen1'));
上一篇 下一篇

猜你喜欢

热点阅读