網站首頁 編程語言 正文
Flutter中的默認導航分成兩種,一種是命名的路由,一種是構建路由。
一、命名路由傳參
應用入口處定義路由表
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false, // 隱藏預覽中的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 {
// 實現路由跳轉
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("返回"),
),
);
}
}
二、構建路由傳參
從HomePage頁面跳轉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: () {
// 實現路由跳轉
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MenuPage(
title: '菜單123',
), // 需要跳轉的頁面
), // 修改路由的名稱、信息等
);
},
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: () {
// 實現路由跳轉
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MenuPage(),
// 修改路由的名稱、信息等
settings: const RouteSettings(
name: '菜單', arguments: {"name": '123'}) // 需要跳轉的頁面
),
);
},
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頁面返回HomePage頁面時,攜帶參數
// 定義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 {
// 實現路由跳轉
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
相關推薦
- 2021-12-15 使用Redis實現令牌桶算法原理解析_Redis
- 2022-06-13 Docker容器數據卷介紹及操作示例_docker
- 2022-06-19 LINQ基礎之Intersect、Except和Distinct子句_C#教程
- 2022-06-08 Springboot+Seata整合以及事務模式分析
- 2024-07-13 ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunnin
- 2022-03-06 C語言之快速排序算法(遞歸Hoare版)介紹_C 語言
- 2022-03-26 C語言實現字符串替換的示例代碼_C 語言
- 2022-12-05 python?os.stat()如何獲取相關文件的系統狀態信息_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支