路由使用

英文文档参考,既然要get的使用,就需要首先再入口处使用 GetMaterialApp

1. GetMaterialApp

GetMaterialApp常用配置参数说明

配置同 MaterialApp 差不多

可选参数名 描述 类型
title 是应用程序的描述 String
defaultTranstion 路由的过渡效果 Transition枚举
initialRoute 默认选中的路由页面 字符串,或者页面类名
unknownRoute 路由找不到显示的页面 GetPage
getPages 路由表配置 List
routingCallback 路由监听回调函数 (Routing route) => { }

1.1 过渡效果配置参数说明

枚举值 描述 使用
fade 从底部向上淡入,从上边向底部淡出 Transition.fade
fadeIn 平面式的淡入淡出 同上
rightToLeft 从右侧向左滑入
leftToRight 从左侧向右滑入
upToDown 从顶部向下进入
downToUp 从下向上进入
rightToLeftWithFade 从右侧带上黑色透明蒙层效果向左滑入
leftToRightWithFade 从左侧带上黑色透明蒙层效果向右滑入
zoom 放大缩小的缩放效果
topLevel 顶部突然放大到正常效果
noTransition 没有过渡效果
cupertino 风格的过渡效果(左右进入退出)
cupertinoDialog ios风格的效果(上下进入退出效果)
size 渐隐渐现效果
native flutter 自带的过渡效果

2. 配置路由和过渡效果

  1. void main() {
  2. runApp(
  3. GetMaterialApp(
  4. debugShowCheckedModeBanner: false,
  5. title: "Application",
  6. defaultTransition: Transition.fade,
  7. initialRoute: "/",
  8. // unknownRoute: GetPage(name: '/notfound', page: () => UnknownRoutePage())
  9. getPages: [
  10. GetPage(name: '/', page: () => HomePage()),
  11. GetPage(name: '/mine', page: () => MinePage()),
  12. ],
  13. ),
  14. );
  15. }

2.1 给某个页面配置过渡效果

  1. GetPage(
  2. name: '/mine',
  3. page: () => MinePage(),
  4. transition: Transition.zoom,
  5. ),

3. 路由跳转

3.1 普通直接跳转

  1. MaterialButton(
  2. onPressed: () {
  3. Get.to(MinePage());
  4. },
  5. color: Colors.blue,
  6. textColor: Colors.white,
  7. child: Text("跳转到mine-page"),
  8. )

3.2 命名路由跳转

  1. MaterialButton(
  2. onPressed: () {
  3. Get.toNamed("/mine");
  4. },
  5. color: Colors.blue,
  6. textColor: Colors.white,
  7. child: Text("跳转到mine-page"),
  8. )

3.3 返回上个页面

  1. MaterialButton(
  2. onPressed: () {
  3. Get.back();
  4. },
  5. color: Colors.blue,
  6. textColor: Colors.white,
  7. child: Text("返回上个页面"),
  8. ),

3.4 Get.off() | Get.offAll() 跳转页面

  • Get.off() 导航到下一个页面并删除前一个页面

  • Get.offAll() 导航到下一个页面并删除以前所有的页面

  • 该方法和 Get.to() 实现的效果一样, 但是使用此方法跳转后,在下一个页面,不能再返回前一个页面,

  • 可以用在 登录页,获取退出页面,这样登录后,或者退出页面后,就不能再返回到前一个页面

  1. MaterialButton(
  2. onPressed: () {
  3. Get.off(MinePage());
  4. },
  5. color: Colors.blue,
  6. textColor: Colors.white,
  7. child: Text("Get.off跳转"),
  8. )

3.5 普通跳转传参

使用 arguments传参数和arguments参数接收

  1. // 传参
  2. MaterialButton(
  3. onPressed: () {
  4. Get.to(MinePage(), arguments: '我是参数');
  5. },
  6. color: Colors.blue,
  7. textColor: Colors.white,
  8. child: Text("跳转传参"),
  9. )

接收参数

  1. class MinePage extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. // 接收参数
  5. String name = Get.arguments;
  6. print("参数:$name");
  7. return Scaffold(
  8. appBar: AppBar(
  9. title: Text("路由管理"),
  10. centerTitle: true,
  11. ),
  12. );
  13. }
  14. }

3.6 命名路由传参

  1. MaterialButton(
  2. onPressed: () {
  3. Get.toNamed('/mine?name=李四');
  4. },
  5. color: Colors.blue,
  6. textColor: Colors.white,
  7. child: Text("命名路由传参"),
  8. )

接收参数

  1. class MinePage extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. // 接收参数 Map类型
  5. String name = Get.parameters['name'];
  6. print("参数:$name");
  7. return Scaffold(
  8. appBar: AppBar(
  9. title: Text("路由管理"),
  10. centerTitle: true,
  11. ),
  12. );
  13. }
  14. }

3.7 命名路由动态传参

第一步:定义动态命名路由

  1. GetPage(name: "/mine/:id", page: () => MinePage()),

第二步:跳转

  1. MaterialButton(
  2. onPressed: () {
  3. // 不允许传中文,会报错
  4. Get.toNamed("/mine/123123");
  5. // Get.toNamed("/mine/中");
  6. },
  7. color: Colors.blue,
  8. textColor: Colors.white,
  9. child: Text("命名路由动态传参"),
  10. )

第三步:接收参数

  1. String id = Get.parameters['id'];
  2. print("参数:$id");

3.8 返回上个页面立即接收携带回来的参数

一块使用 await Get.to() 和 Get.back(result: 456) 实现

  • 第一步:跳转某个页面,并接受数据
  1. MaterialButton(
  2. onPressed: () async {
  3. var data = await Get.to(MinePage());
  4. // 上个页面返回后,立即拿到数据,456
  5. print(data);
  6. },
  7. color: Colors.blue,
  8. textColor: Colors.white,
  9. child: Text("await Get.to"),
  10. )
  • 第二步:携带参数返回上个页面
  1. MaterialButton(
  2. onPressed: () {
  3. // 返回携带参数
  4. Get.back(result: 456);
  5. },
  6. color: Colors.blue,
  7. textColor: Colors.white,
  8. child: Text("返回上个页面"),
  9. ),

3.9 Get.offNamed 和 Get.offAllNamed

Get.offNamed () 命名路由导航到下一个页面并删除前一个页面

  1. MaterialButton(
  2. onPressed: () {
  3. Get.offNamed ("/mine");
  4. },
  5. color: Colors.blue,
  6. textColor: Colors.white,
  7. child: Text("Get.offNamed"),
  8. )

4. 路由中间件routingCallback

routingCallback: 路由监听回调

  1. GetMaterialApp(
  2. debugShowCheckedModeBanner: false,
  3. title: "Application",
  4. defaultTransition: Transition.fade,
  5. initialRoute: "/",
  6. getPages: [
  7. GetPage(name: '/', page: () => HomePage()),
  8. GetPage(name: "/mine/:id", page: () => MinePage()),
  9. GetPage(name: "/news", page: () => NewsPage()),
  10. GetPage(
  11. name: '/mine',
  12. page: () => MinePage(),
  13. transition: Transition.zoom,
  14. ),
  15. ],
  16. routingCallback: (Routing route) {
  17. print(route.current);
  18. },
  19. ),

5. snackbar 提示框

配置参数太多,就自己看文档吧

  1. MaterialButton(
  2. onPressed: () {
  3. Get.snackbar(
  4. Get.snackbar(
  5. "提示", // title
  6. "我是提示的信息", // message
  7. icon: Icon(Icons.home), // 头部的图标
  8. shouldIconPulse: true,
  9. barBlur: 10,
  10. colorText: Colors.white, // 字体颜色
  11. isDismissible: true,
  12. snackPosition: SnackPosition.BOTTOM, // 显示的位置
  13. duration: Duration(seconds: 3), // 显示时长
  14. titleText: Text("自定义标题"), // 会覆盖掉 第一个 title的参数的效果
  15. messageText: Text("自定义提示语"), // 会覆盖掉 第二个 message的参数的效果
  16. maxWidth: 200.2, // 提示框的最大宽度
  17. margin: EdgeInsets.all(100), // 提示框距离四周的距离
  18. padding: EdgeInsets.all(10), // 提示框的内边距
  19. borderRadius: 20.2, // 提示框四周的圆角
  20. borderColor: Colors.red, // 提示框边框的颜色,必须和borderWidth一块用,单独使用会报错
  21. borderWidth: 2.2, // 提示框边框大小
  22. backgroundColor: Colors.black54, // 背景色
  23. leftBarIndicatorColor: Colors.blue, // 左侧边上一条竖杆的背景色,并不是边框
  24. boxShadows: [
  25. BoxShadow(color: Colors.red, offset: Offset(10, 20)),
  26. ], // 添加阴影效果
  27. onTap: (GetBar<Object> a) {
  28. print(a);
  29. }, // 提示框点击回调
  30. );
  31. );
  32. },
  33. color: Colors.red,
  34. textColor: Colors.white,
  35. child: Text("snackbar"),
  36. ),

6. dialogs 弹窗

6.1 默认弹窗效果

  • Get.defaultDialog() 打开弹窗

  • 关闭弹窗: 调用路由的 Get.back() 进行关闭, 配置太多了,自己看源码吧

  1. MaterialButton(
  2. onPressed: () {
  3. Get.defaultDialog(
  4. title: "您确定要这样吗", // 弹窗的标题
  5. titleStyle: TextStyle(color: Colors.blue), // 弹窗的标题的样式
  6. onConfirm: () => Get.back(), // 确定回调
  7. onCancel: () => Get.back(), // 取消的回调
  8. confirmTextColor: Colors.purple, // 确定按钮的回调字体颜色
  9. cancelTextColor: Colors.red, // 取消的回调字体颜色
  10. textConfirm: "确定", // 确定按钮 文字
  11. textCancel: "取消", // 取消按钮 文字
  12. confirm: Text("自定义确定按钮"), // 自定义确定按钮, 需要自己写关闭函数,比较灵活
  13. cancel: MaterialButton(
  14. onPressed: Get.back,
  15. color: Colors.blue,
  16. child: Text("自定义取消按钮"),
  17. ), // 自定义取消按钮
  18. middleTextStyle: TextStyle(
  19. color: Colors.blue,
  20. ), // 中间的提示语的样式,自定义content参数时无效
  21. middleText: "Dialog made in 3 lines of code", // 中间的提示语
  22. backgroundColor: Colors.greenAccent, // 整个背景色
  23. buttonColor: Colors.red, // 确定按钮的背景色,自定义按钮时无效
  24. radius: 5, // 弹窗的圆角效果
  25. actions: [
  26. Icon(Icons.ac_unit),
  27. Icon(Icons.ac_unit),
  28. Icon(Icons.ac_unit),
  29. Icon(Icons.ac_unit),
  30. ], // 同appBar上action 效果一样
  31. content:
  32. Text("我是自定义中间显示的widget"), // 自定义中间显示的UI,会覆盖掉middleText的效果
  33. );
  34. },
  35. color: Colors.red,
  36. textColor: Colors.white,
  37. child: Text("打开默认的Dialogs"),
  38. ),

6.2 自定义的弹窗

  • 默认高度铺满全屏 Get.dialog()
  1. MaterialButton(
  2. onPressed: () {
  3. Get.dialog(
  4. Container(
  5. color: Colors.black26,
  6. child: Text("我是文字"),
  7. ),
  8. );
  9. },
  10. color: Colors.red,
  11. textColor: Colors.white,
  12. child: Text("打开自定义的Dialogs"),
  13. ),

6.3 generalDialog 弹窗

  • 效果同自定义弹窗,也是铺满全屏
  1. Get.generalDialog(
  2. pageBuilder: (BuildContext context,
  3. Animation<double> animation,
  4. Animation<double> secondaryAnimation) {
  5. return Container(
  6. color: Colors.red,
  7. );
  8. },
  9. );

7. bottomSheet

  • 调用 Get.back() 关闭弹窗
  1. Get.bottomSheet(
  2. Container(
  3. child: Wrap(
  4. children: <Widget>[
  5. ListTile(
  6. leading: Icon(Icons.music_note),
  7. title: Text('Music'),
  8. onTap: () {Get.back()}),
  9. ListTile(
  10. leading: Icon(Icons.videocam),
  11. title: Text('Video'),
  12. onTap: () {},
  13. ),
  14. ],
  15. ),
  16. ),
  17. backgroundColor: Colors.green, // 底部bottomSheet的背景色
  18. elevation: 10.0,
  19. );
  20. );