123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- import 'package:flutter/foundation.dart';
- import 'package:get/get.dart';
- import 'package:router/my_router_history_manager.dart';
- enum LaunchModel {
- standard, //默认模式
- singleTop, //检查栈顶
- singleTask, //检查全栈
- }
- extension GetRouterNavigation on GetInterface {
- /// 查询指定的RouterName是否存在自己的路由栈中
- bool isRouteExist(String routerName) {
- return MyRouterHistoryManager().isRouteExist(routerName);
- }
- /// 查询指定的RouterName是否存在自己的路由栈顶
- bool isTopRouteExist(String routerName) {
- return MyRouterHistoryManager().isTopRouteExist(routerName);
- }
- /// 跳转页面SingleTask模式
- void _toNamedSingleTask(
- String routerName, {
- dynamic arguments,
- bool preventDuplicates = false,
- void Function(dynamic value)? cb,
- Map<String, String>? parameters,
- }) {
- if (isRouteExist(routerName)) {
- Get.until((route) => route.settings.name == routerName);
- } else {
- Get.offNamed(
- routerName,
- arguments: arguments,
- parameters: parameters,
- preventDuplicates: preventDuplicates,
- )?.then((value) => {
- if (cb != null) {cb(value)}
- });
- }
- }
- /// 跳转页面SingleTop模式
- void _toNamedSingleTop(
- String routerName, {
- dynamic arguments,
- bool preventDuplicates = false,
- void Function(dynamic value)? cb,
- Map<String, String>? parameters,
- }) async {
- if (isTopRouteExist(routerName)) {
- Get.until((route) => route.settings.name == routerName);
- } else {
- Get.toNamed(
- routerName,
- arguments: arguments,
- parameters: parameters,
- preventDuplicates: preventDuplicates,
- )?.then((value) => {
- if (cb != null) {cb(value)}
- });
- }
- }
- /// 默认的跳转方式,带自己的回调
- void _toNamedStandard(
- String routerName, {
- dynamic arguments,
- bool preventDuplicates = false,
- void Function(dynamic value)? cb,
- Map<String, String>? parameters,
- }) {
- Get.toNamed(
- routerName,
- arguments: arguments,
- parameters: parameters,
- preventDuplicates: preventDuplicates,
- )?.then((value) => {
- if (cb != null) {cb(value)}
- });
- }
- /*
- * 启动新页面
- */
- void start(
- String routerName, {
- LaunchModel launchModel = LaunchModel.standard,
- dynamic arguments,
- bool preventDuplicates = false, //默认多实例
- void Function(dynamic value)? cb,
- Map<String, String>? parameters,
- }) {
- //检查栈顶
- if (launchModel == LaunchModel.singleTop) {
- _toNamedSingleTop(
- routerName,
- arguments: arguments,
- cb: cb,
- parameters: parameters,
- preventDuplicates: preventDuplicates,
- );
- //检查全栈
- } else if (launchModel == LaunchModel.singleTask) {
- _toNamedSingleTask(
- routerName,
- arguments: arguments,
- cb: cb,
- parameters: parameters,
- preventDuplicates: preventDuplicates,
- );
- //默认启动
- } else {
- _toNamedStandard(
- routerName,
- arguments: arguments,
- cb: cb,
- parameters: parameters,
- preventDuplicates: preventDuplicates,
- );
- }
- }
- /*
- * 关闭当前启动新页面
- */
- void startWithPop(
- String routerName, {
- dynamic arguments,
- bool preventDuplicates = false, //默认多实例
- void Function(dynamic value)? cb,
- Map<String, String>? parameters,
- }) {
- Get.offNamed(
- routerName,
- arguments: arguments,
- parameters: parameters,
- preventDuplicates: preventDuplicates,
- )?.then((value) => {
- if (cb != null) {cb(value)}
- });
- }
- /*
- * 关闭页面,调用Get的back方法
- */
- void pop<T>({
- T? result,
- bool closeOverlays = false,
- bool canPop = true,
- int? id,
- }) {
- Get.back(result: result, closeOverlays: closeOverlays, canPop: canPop, id: id);
- }
- /// 通知栏点击跳转到指定页面
- void toNamedByNotification(
- String routerName, {
- dynamic arguments,
- void Function(dynamic value)? cb,
- Map<String, String>? parameters,
- }) async {
- if (MyRouterHistoryManager().routeNames.isEmpty) {
- //冷启动
- if (!kReleaseMode){
- print('应用没启动,使用LaunchUrl的方式');
- }
- // String url = 'app://router?name=$routerName';
- // if (await canLaunch(url)) {
- // await launch(url);
- // } else {
- // Log.d('无法启动此scheme-host');
- // }
- } else {
- //正常跳转
- if (!kReleaseMode){
- print('应用已启动,使用默认路由方式');
- }
- _toNamedSingleTask(routerName);
- }
- }
- }
|