Flutter Navigator路由传参的实现
目录
一、命名路由传参二、构建路由传参第一种方式:第二种方式: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("返回"),
),
);
}
}
到此这篇关于Flutter Navigator路由传参的实现的文章就介绍到这了,更多相关Flutter Navigator路由传参内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
下一篇:详解C#中Helper类的使用
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?

