天天看點

java通路頁面路由_Flutter Navigator路由,傳回,替換路由及傳回指定頁面

檢視.

Navigator.pop(context);   //最簡單的傳回

Navigator.popAndPushNamed(context, routeName);    //退出目前頁面,并添加新的頁面 (先删後加)

Navigator.pushNamedAndRemoveUntil( context, newRouteName, (route) => false); //銷毀棧内所有頁面并跳轉

Navigator.pushReplacement(context, newRoute);  //将跳轉頁替換掉目前頁 (先加後删)

Navigator.pushReplacementNamed(context, routeName)   //以命名路由方式跳轉 同上

使用路由跳轉到頁面後 傳回其原來頁面的操作

1.pop 傳回

Navigator.pop(context);

回到原頁面,相當于點選傳回按鈕

2.替換路由

Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => intentPage(),));

Navigator.pushReplacementNamed(context, "/intentPage");

以命名方式跳轉 必須在首頁 main 的MaterialApp中聲明命名

child: MaterialApp(

routes: {

"/intentPage":(context)=>intentPage(),

},

));

替換路由 intentPage為要跳轉的頁面(page3) 可以了解為:page1=>page2=>page3 ,用page3替換page2 傳回直接跳轉到page1

3.傳回到指定頁面 (棧内頁面全部銷毀,然後将傳回頁面置頂)

"/"為命名路由 route == null 将站内頁面銷毀 并添加跳轉頁面

onPressed: () {

Navigator.pushNamedAndRemoveUntil(

context, "/", (route) => route == null);

},

或另一種寫法

onPressed: () {

Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => BottomNavigatorTest(),), (route) => route == null)

},

結尾

還有兩個不太常用的

Navigator.maybePop(context); //能退出則退出 首頁調用maybePop()是不會退出的

Navigator.of(context).canPop();會傳回一個boolean值,表示目前頁面是否可以退出

本文位址:https://blog.csdn.net/qq_42972848/article/details/107893017

希望與廣大網友互動??

點此進行留言吧!