路由管理.png
路由.png
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: const PageA(),
//打开App时进入的页面
initialRoute: "pageA",
//注册命名路由
routes: {
"pageA": (context) => const PageA(),
},
));
}
class PageA extends StatelessWidget {
const PageA({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("PageA"),
),
body: Center(
child: ElevatedButton(
child: const Text("去页面B"),
onPressed: () async {
var rst = await Navigator.push(context,
MaterialPageRoute(builder: (context) {
return const PageB();
}));
print("返回参数: $rst");
},
),
),
);
}
}
class PageB extends StatelessWidget {
const PageB({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("PageB"),
),
body: Column(
children: [
ElevatedButton(
child: const Text("去页面C"),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return const PageC();
}));
},
),
ElevatedButton(
child: const Text("返回页面A"),
onPressed: () {
//返回上一页面
Navigator.pop(context, "返回参数");
},
),
],
),
);
}
}
class PageC extends StatelessWidget {
const PageC({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("PageC"),
),
body: Center(
child: ElevatedButton(
child: const Text("去页面A"),
onPressed: () {
//使用命名路由
Navigator.pushReplacementNamed(context, "pageA");
},
),
),
);
}
}
网友评论