flutter —— 使用 getx 进行路由管理
路由
基础方法
Get.toNamed("/NextScreen");
Get.offNamed("/NextScreen");
Get.offAllNamed("/NextScreen");
路由传参
Get.toNamed("/NextScreen", arguments: 'Get is the best');
print(Get.arguments);
//print out: Get is the best
命令路由
void main() {
runApp(
GetMaterialApp(
initialRoute: '/',
getPages: [
GetPage(
name: '/',
page: () => MyHomePage(),
),
GetPage(
name: '/profile/',
page: () => MyProfile(),
),
//你可以为有参数的路由定义一个不同的页面,也可以为没有参数的路由定义一个不同的页面,但是你必须在不接收参数的路由上使用斜杠"/",就像上面说的那样。
GetPage(
name: '/profile/:user',
page: () => UserProfile(),
),
GetPage(
name: '/third',
page: () => Third(),
transition: Transition.cupertino
),
],
)
);
}
// 跳转路由
Get.toNamed("/second/34954");
// 获取路由参数
print(Get.parameters['user']);
// out: 34954
返回路由传值
在购物车、订单中很有用
// 导航到该路由,通过 await 获取回调结果 var data = await Get.to(Payment()); if(data == 'success') madeAnything(); // 传递回调内容 Get.back(result: 'success');
免 context 导航
Snackbar
Get.snackbar('Hi', 'i am a modern snackbar');
也可以使用 Get.rawSnackbar()
Dialog
Get.dialog(YourDialogWidget());
Get.defaultDialog(
onConfirm: () => print("Ok"),
middleText: "Dialog made in 3 lines of code"
);
// 还可以使用 Get.generalDialog 代替 showGeneralDialog
BottomSheet
Get.bottomSheet(
Container(
child: Wrap(
children: <Widget>[
ListTile(
leading: Icon(Icons.music_note),
title: Text('Music'),
onTap: () {}
),
ListTile(
leading: Icon(Icons.videocam),
title: Text('Video'),
onTap: () {},
),
],
),
)
);
2333
浙公网安备 33010602011771号