ext_get_nav.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import 'package:flutter/foundation.dart';
  2. import 'package:get/get.dart';
  3. import 'package:router/my_router_history_manager.dart';
  4. enum LaunchModel {
  5. standard, //默认模式
  6. singleTop, //检查栈顶
  7. singleTask, //检查全栈
  8. }
  9. extension GetRouterNavigation on GetInterface {
  10. /// 查询指定的RouterName是否存在自己的路由栈中
  11. bool isRouteExist(String routerName) {
  12. return MyRouterHistoryManager().isRouteExist(routerName);
  13. }
  14. /// 查询指定的RouterName是否存在自己的路由栈顶
  15. bool isTopRouteExist(String routerName) {
  16. return MyRouterHistoryManager().isTopRouteExist(routerName);
  17. }
  18. /// 跳转页面SingleTask模式
  19. void _toNamedSingleTask(
  20. String routerName, {
  21. dynamic arguments,
  22. bool preventDuplicates = false,
  23. void Function(dynamic value)? cb,
  24. Map<String, String>? parameters,
  25. }) {
  26. if (isRouteExist(routerName)) {
  27. Get.until((route) => route.settings.name == routerName);
  28. } else {
  29. Get.offNamed(
  30. routerName,
  31. arguments: arguments,
  32. parameters: parameters,
  33. preventDuplicates: preventDuplicates,
  34. )?.then((value) => {
  35. if (cb != null) {cb(value)}
  36. });
  37. }
  38. }
  39. /// 跳转页面SingleTop模式
  40. void _toNamedSingleTop(
  41. String routerName, {
  42. dynamic arguments,
  43. bool preventDuplicates = false,
  44. void Function(dynamic value)? cb,
  45. Map<String, String>? parameters,
  46. }) async {
  47. if (isTopRouteExist(routerName)) {
  48. Get.until((route) => route.settings.name == routerName);
  49. } else {
  50. Get.toNamed(
  51. routerName,
  52. arguments: arguments,
  53. parameters: parameters,
  54. preventDuplicates: preventDuplicates,
  55. )?.then((value) => {
  56. if (cb != null) {cb(value)}
  57. });
  58. }
  59. }
  60. /// 默认的跳转方式,带自己的回调
  61. void _toNamedStandard(
  62. String routerName, {
  63. dynamic arguments,
  64. bool preventDuplicates = false,
  65. void Function(dynamic value)? cb,
  66. Map<String, String>? parameters,
  67. }) {
  68. Get.toNamed(
  69. routerName,
  70. arguments: arguments,
  71. parameters: parameters,
  72. preventDuplicates: preventDuplicates,
  73. )?.then((value) => {
  74. if (cb != null) {cb(value)}
  75. });
  76. }
  77. /*
  78. * 启动新页面
  79. */
  80. void start(
  81. String routerName, {
  82. LaunchModel launchModel = LaunchModel.standard,
  83. dynamic arguments,
  84. bool preventDuplicates = false, //默认多实例
  85. void Function(dynamic value)? cb,
  86. Map<String, String>? parameters,
  87. }) {
  88. //检查栈顶
  89. if (launchModel == LaunchModel.singleTop) {
  90. _toNamedSingleTop(
  91. routerName,
  92. arguments: arguments,
  93. cb: cb,
  94. parameters: parameters,
  95. preventDuplicates: preventDuplicates,
  96. );
  97. //检查全栈
  98. } else if (launchModel == LaunchModel.singleTask) {
  99. _toNamedSingleTask(
  100. routerName,
  101. arguments: arguments,
  102. cb: cb,
  103. parameters: parameters,
  104. preventDuplicates: preventDuplicates,
  105. );
  106. //默认启动
  107. } else {
  108. _toNamedStandard(
  109. routerName,
  110. arguments: arguments,
  111. cb: cb,
  112. parameters: parameters,
  113. preventDuplicates: preventDuplicates,
  114. );
  115. }
  116. }
  117. /*
  118. * 关闭当前启动新页面
  119. */
  120. void startWithPop(
  121. String routerName, {
  122. dynamic arguments,
  123. bool preventDuplicates = false, //默认多实例
  124. void Function(dynamic value)? cb,
  125. Map<String, String>? parameters,
  126. }) {
  127. Get.offNamed(
  128. routerName,
  129. arguments: arguments,
  130. parameters: parameters,
  131. preventDuplicates: preventDuplicates,
  132. )?.then((value) => {
  133. if (cb != null) {cb(value)}
  134. });
  135. }
  136. /*
  137. * 关闭页面,调用Get的back方法
  138. */
  139. void pop<T>({
  140. T? result,
  141. bool closeOverlays = false,
  142. bool canPop = true,
  143. int? id,
  144. }) {
  145. Get.back(result: result, closeOverlays: closeOverlays, canPop: canPop, id: id);
  146. }
  147. /// 通知栏点击跳转到指定页面
  148. void toNamedByNotification(
  149. String routerName, {
  150. dynamic arguments,
  151. void Function(dynamic value)? cb,
  152. Map<String, String>? parameters,
  153. }) async {
  154. if (MyRouterHistoryManager().routeNames.isEmpty) {
  155. //冷启动
  156. if (!kReleaseMode){
  157. print('应用没启动,使用LaunchUrl的方式');
  158. }
  159. // String url = 'app://router?name=$routerName';
  160. // if (await canLaunch(url)) {
  161. // await launch(url);
  162. // } else {
  163. // Log.d('无法启动此scheme-host');
  164. // }
  165. } else {
  166. //正常跳转
  167. if (!kReleaseMode){
  168. print('应用已启动,使用默认路由方式');
  169. }
  170. _toNamedSingleTask(routerName);
  171. }
  172. }
  173. }