GetMaterialApp( // Before: MaterialApp(
home: MyHome(),
)
说明:
Navigator.of(context).push(
context,
MaterialPageRoute(
builder: (BuildContext context) {
return HomePage();
},
),
);
navigator?.push(MaterialPageRoute(
builder: (_){
return Home();
}
));
Get.to(Home());
void main() => runApp(GetMaterialApp(
initialRoute: '/',
getPages: [
GetPage(name: '/NextScreen', page: ()=>NextScreen()),
],
home: Home()));
1-在 GetMaterialApp 中定义一个 unknownRoute 页面
void main() => runApp(GetMaterialApp(
unknownRoute: GetPage(name: '/notfound', page: () => UnknownRoutePage()),
initialRoute: '/',
getPages: [
GetPage(name: '/NextScreen', page: ()=>NextScreen()),
],
home: Home()));
2- 通过named方式跳转时,路由出错时,跳转到UnknownRoutePage页面
levatedButton(child: Text("Error"), onPressed: () => Get.toNamed("/NextScreen1")),
说明:NextScreen1找不到此路径,出现问题时,交由UnknownRoutePage显示处理
通过arguments方式指定
Get.toNamed("/NextScreen", arguments: 'Get is the best');
通过named方式,放在路由页面的后面用问号间隔开,比如
Get.offAllNamed("/NextScreen?device=phone&id=354&name=Enzo");
通过arguments方式传递的,值接受方式
var argument= Get.arguments;
通过named方式传递的,值接受方式
var device=Get.parameters['device'];
var id= Get.parameters['id'];
var name= Get.parameters['name'];
routingCallback
回调routingCallback
回调中,进行业务逻辑的处理void main() => runApp(GetMaterialApp(
unknownRoute: GetPage(name: '/notfound', page: () => UnknownRoutePage()),
routingCallback: (routing) {
if(routing?.current == '/second'){
openAds();
}
},
initialRoute: '/',
getPages: [
GetPage(name: '/NextScreen', page: ()=>NextScreen()),
GetPage(name: '/second', page: ()=>Second())
],
home: Home()));
void openAds()
{
print('ads');
}
ElevatedButton(child: Text("MiddleWare"), onPressed: () => Get.toNamed('/second')),
说明:使用Get.to(Second());方法时,注意Second的大小写
事件被触发,ads被打印
导航嵌套-1 | 导航嵌套-2 |
---|---|
Navigator(
key: Get.nestedKey(1), // create a key by index
initialRoute: '/',
onGenerateRoute: (settings) {
if (settings.name == '/') {
return GetPageRoute(
page: () => Scaffold(
appBar: AppBar(
title: Text("Main"),
),
body: Center(
child: TextButton(
color: Colors.blue,
onPressed: () {
Get.toNamed('/second', id:1); // navigate by your nested route by index
},
child: Text("Go to second"),
),
),
),
);
} else if (settings.name == '/second') {
return GetPageRoute(
page: () => Center(
child: Scaffold(
appBar: AppBar(
title: Text("Main"),
),
body: Center(
child: Text("second")
),
),
),
);
}
}
),