1. Get.to()
Get.to()
方法用于直接导航到指定的页面,但不从路由栈中移除当前页面。
dartGet.to(NextScreen());
2. Get.toNamed()
Get.toNamed()
方法使用路由名称进行导航,与 GetPage
定义的路由名称相对应。
dartGet.toNamed('/nextScreen');
在 GetMaterialApp
或 GetCupertinoApp
的 getPages
中,你需要先定义这个路由名称:
dartGetMaterialApp(
getPages: [
GetPage(
name: '/nextScreen',
page: () => NextScreen(),
),
// ... 其他GetPage定义
],
// ... 其他参数
)
3. Get.off()
Get.off()
方法导航到新的页面,并从路由栈中移除当前页面(包括当前页面)。
dartGet.off(NextScreen());
4. Get.offNamed()
Get.offNamed()
方法与 Get.off()
类似,但使用路由名称进行导航。
dartGet.offNamed('/nextScreen');
5. Get.offAll()
Get.offAll()
方法导航到新的页面,并从路由栈中移除所有页面(只留下新的页面)。
dartGet.offAll(NextScreen());
6. Get.offAllNamed()
Get.offAllNamed()
方法与 Get.offAll()
类似,但使用路由名称进行导航。
dartGet.offAllNamed('/nextScreen');
7. 传递参数
在路由跳转时,你还可以传递参数给目标页面。这可以通过在目标页面的构造函数中定义参数来实现,并在导航时传递这些参数。
dart// 定义带参数的页面
class NextScreen extends GetView<NextController> {
final String param;
NextScreen({Key? key, required this.param}) : super(key: key);
// ... 其他代码
}
// 导航时传递参数
Get.to(() => NextScreen(param: 'Hello World!'));
或者使用命名路由和参数:
dart// 在GetPage中定义带参数的路由
GetPage(
name: '/nextScreen/:id',
page: () => NextScreen(),
),
// 导航时传递参数
Get.toNamed('/nextScreen/123');
// 在NextScreen中获取参数
class NextScreen extends GetView<NextController> {
final String id;
NextScreen({Key? key, required this.id}) : super(key: key);
// 使用 Get.parameters['id'] 来获取参数值
void initState() {
super.initState();
String idValue = Get.parameters['id'];
// ... 处理idValue
}
// ... 其他代码
}