1 动态路由
跳转页面代码
Navigator.push<void>(
context,
MaterialPageRoute<void>(
builder: (BuildContext context) => HomePageB(),
),
);
返回页面代码
Navigator.pop(context);
跳转页面传参数,通过构造方法传参
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => HomePageB(
htitle: "pageB",
),
),
).then((value){
print(value);
});
回调参数
Navigator.pop(context,"get it");
2 静态路由 在MaterialApp 内设置路由
return MaterialApp(
title: "路由导航学习",
theme:
ThemeData(primarySwatch: Colors.blue, backgroundColor: Colors.white),
home: HomePage(htitle: "pageA"),
routes: {
"/home":(context) => HomePage(htitle: "pageA"),
"/pageb":(context) => HomePageB(htitle: "pageB")
},
onGenerateRoute: (RouteSettings settings){
print(settings.name);
return null;
},
onUnknownRoute: (RouteSettings settings){//未找到跳转页面时跳转
return MaterialPageRoute(builder: (BuildContext context) =>
HomePageB(htitle: "404"));
},
);
跳转页面
Navigator.pushNamed(context, "/pageb",arguments: "title").then((value) => print(value));
接收参数
var title = ModalRoute.of(context)?.settings.arguments;
回调参数
Navigator.pop(context,"get it");
|