網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Flutter中的默認(rèn)導(dǎo)航分成兩種,一種是命名的路由,一種是構(gòu)建路由。
一、命名路由傳參
應(yīng)用入口處定義路由表
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false, // 隱藏預(yù)覽中的debug
title: 'Flutter Demo',
routes: {
'/': (context) => const HomePage(),
"menu": (context) => const MenuPage()
},
);
}
}
// 定義HomePage
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("登錄"),
),
body: ElevatedButton(
onPressed: () async {
// 實(shí)現(xiàn)路由跳轉(zhuǎn)
var result = await Navigator.pushNamed(context, 'menu',
arguments: {'name': 'title'});
print(result);
},
child: const Text('登錄'),
),
);
}
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
const MenuPage({Key? key}) : super(key: key);
@override
// 接收傳參
Widget build(BuildContext context) {
dynamic argumentsData = ModalRoute.of(context)?.settings.arguments;
return Scaffold(
appBar: AppBar(
title: Text('菜單' + argumentsData.toString()),
),
body: ElevatedButton(
onPressed: () {
Navigator.pop(context, {'name': "Navigator.pop傳參"});
},
child: const Text("返回"),
),
);
}
}
二、構(gòu)建路由傳參
從HomePage頁(yè)面跳轉(zhuǎn)MenuPage頁(yè)面時(shí),攜帶參數(shù)
第一種方式:
// 定義HomePage
class HomePage extends StatelessWidget {
const HomePage ({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("登錄"),
),
body: ElevatedButton(
onPressed: () {
// 實(shí)現(xiàn)路由跳轉(zhuǎn)
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MenuPage(
title: '菜單123',
), // 需要跳轉(zhuǎn)的頁(yè)面
), // 修改路由的名稱、信息等
);
},
child: const Text('登錄'),
),
);
}
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
// 定義接收的字段
final String title;
const MenuPage({Key? key, required this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("返回"),
),
);
}
}
第二種方式:
// 定義HomePage
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("登錄"),
),
body: ElevatedButton(
onPressed: () {
// 實(shí)現(xiàn)路由跳轉(zhuǎn)
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MenuPage(),
// 修改路由的名稱、信息等
settings: const RouteSettings(
name: '菜單', arguments: {"name": '123'}) // 需要跳轉(zhuǎn)的頁(yè)面
),
);
},
child: const Text('登錄'),
),
);
}
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
const MenuPage({Key? key}) : super(key: key);
@override
// 接收傳參
Widget build(BuildContext context) {
dynamic argumentsData = ModalRoute.of(context)?.settings.arguments;
return Scaffold(
appBar: AppBar(
title: Text('菜單' + argumentsData.toString()),
),
body: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("返回"),
),
);
}
}
從MenuPage頁(yè)面返回HomePage頁(yè)面時(shí),攜帶參數(shù)
// 定義HomePage
class HomePage extends StatelessWidget {
const HomePage ({Key? key}) : super(key: key);;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("登錄"),
),
body: ElevatedButton(
onPressed: () async {
// 實(shí)現(xiàn)路由跳轉(zhuǎn)
var result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MenuPage(),
),
);
print(result);
},
child: const Text('登錄'),
),
);
}
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
const MenuPage({Key? key}) : super(key: key);
@override
// 接收傳參
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('菜單'),
),
body: ElevatedButton(
onPressed: () {
Navigator.pop(context, {'name': "Navigator.pop傳參"});
},
child: const Text("返回"),
),
);
}
}
原文鏈接:https://blog.csdn.net/qq_16221009/article/details/123347768
相關(guān)推薦
- 2023-06-16 Golang調(diào)用FFmpeg轉(zhuǎn)換視頻流的實(shí)現(xiàn)_Golang
- 2022-11-02 Android啟動(dòng)初始化方案App?StartUp的應(yīng)用詳解_Android
- 2022-02-23 Linux用戶建立腳本/猜字游戲/網(wǎng)卡流量監(jiān)控介紹_Linux
- 2022-06-02 redis?sentinel監(jiān)控高可用集群實(shí)現(xiàn)的配置步驟_Redis
- 2022-12-22 Go語(yǔ)言編程通過(guò)dwarf獲取內(nèi)聯(lián)函數(shù)_Golang
- 2022-04-28 WPF依賴屬性用法詳解_實(shí)用技巧
- 2022-08-31 .Net插件框架Managed?Extensibility?Framework簡(jiǎn)介_實(shí)用技巧
- 2023-07-16 oracle 加解密函數(shù)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支