pushGlobal static method

dynamic pushGlobal(
  1. GlobalKey<NavigatorState> navigatorKey,
  2. Widget scene
)

从window获取NA传递的路由参数 从根结点跳转页面

Implementation

// static Map<String, dynamic> parseRouter(Window window){
//   // window.defaultRouteName就是获取Android传递过来的参数
//   // 通过这个字段我们就可以进行Flutter页面的路由的分发
//   String url = window.defaultRouteName;
//   // route名称,路由path路径名称
//   String route = url.indexOf('?') == -1 ? url : url.substring(0, url.indexOf('?'));
//   // 参数Json字符串
//   String paramsJson = url.indexOf('?') == -1 ? '{}' : url.substring(url.indexOf('?') + 1);
//   // 解析参数
//   Map<String, dynamic> params = json.decode(paramsJson);
//   LogUtils.d('path---->' + route + " " + params.toString());
//   params["route"] = route;
//   return params;
// }

// 什么是路由管理???
// 简单俩说,导航管理都会维护一个路由栈,
// 路由入栈(push)操作对应打开一个新页面,
// 路由出栈(pop)操作对应页面关闭操作,
// 而路由管理主要是指如何来管理路由栈。
/// 从根结点跳转页面
static pushGlobal(GlobalKey<NavigatorState> navigatorKey, Widget scene) {
  // Another exception was thrown:
  // Navigator operation requested with a context that does not include a Navigator.
  // 因为flutter会根据这个context一直上溯,一直到根节点的widget,
  // 注意,上溯是根据context的,会上溯到这个context相关的widget的最根节点
  // 导航到新路由
  try {
    //执行build方法
    var currentState = navigatorKey.currentState;
    currentState?.push(MaterialPageRoute(
      builder: (BuildContext context) => scene,
    ),);
  } catch (e, stack) {
    // 有异常时则弹出错误提示
    LogUtils.e("路由异常"+e.toString()+"-----"+stack.toString());
  }
}