Flutter路由工具类RouteUtils,可二次开发,拿来即用

一、RouteUtils路由核心类

Dart 复制代码
/**
 * 路由封装
 */
class RouteUtils {

  RouteUtils._();

  static final navigatorKey = GlobalKey<NavigatorState>();

// App 根节点Context
  static BuildContext get context => navigatorKey.currentContext!;

  static NavigatorState get navigator => navigatorKey.currentState!;

  ///普通动态跳转-->page
  static Future push(
    BuildContext context,
    Widget page, {
    bool? fullscreenDialog,
    RouteSettings? settings,
    bool maintainState = true,
  }) {
    return Navigator.push(
        context,
        MaterialPageRoute(
          builder: (_) => page,
          fullscreenDialog: fullscreenDialog ?? false,
          settings: settings,
          maintainState: maintainState,
        ));
  }

  //常规路由导航
  static Future pushForName(
    BuildContext context,
    String routeName, {
    Object? arguments,
  }) {
    return Navigator.pushNamed(context, routeName, arguments: arguments);
  }

  ///白定义route动态跳转
  static Future pushForPageRoute(BuildContext context, Route route) {
    return Navigator.push(context, route);
  }

  //清空栈,只留目标页面
  static Future pushNamedAndRemoveUntil(
    BuildContext context,
    String name, {
    Object? arguments,
  }) {
    return Navigator.pushNamedAndRemoveUntil(context, name, (route) => false,
        arguments: arguments);
  }

  //清空栈,只留目标页面
  static Future pushAndRemoveUntil(
    BuildContext context,
    Widget page, {
    bool? fullscreenDialog,
    RouteSettings? settings,
    bool maintainState = true,
  }) {
    return Navigator.pushAndRemoveUntil(
        context,
        MaterialPageRoute(
          builder: (_) => page,
          fullscreenDialog: fullscreenDialog ?? false,
          settings: settings,
          maintainState: maintainState,
        ),
        (route) => false);
  }

  //用新的路由替换当路由
  static Future pushReplacement(BuildContext context, Route route,
      {Object? result}) {
    return Navigator.pushReplacement(context, route, result: result);
  }

  //用新的路由替换当路由
  static Future pushReplacementNamed(
    BuildContext context,
    String name, {
    Object? result,
    Object? arguments,
  }) {
    return Navigator.pushReplacementNamed(context, name,
        arguments: arguments, result: result);
  }

  //关闭当前页面
  static void pop(BuildContext context) {
    Navigator.pop(context);
  }

  ///关闭当前页面:包含返回值
  static void popOfData<T extends Object?>(BuildContext context, {T? data}) {
    Navigator.of(context).pop(data);
  }
}

二、Routes辅助类(可用可不用)

Dart 复制代码
class Routes {

  static MaterialPageRoute _jumpRoutePage(
    Widget pageWidget, {
    RouteSettings? settings,
    bool maintainState = true,
    bool fullscreenDialog = false,
    bool allowSnapshotting = true,
  }) {
    return MaterialPageRoute(
        builder: (BuildContext context) {
          return pageWidget;
        },
        settings: settings,
        maintainState: maintainState,
        fullscreenDialog: fullscreenDialog,
        allowSnapshotting: allowSnapshotting);
  }
}

三、RoutePath路径配置类(可用可不用)

Dart 复制代码
class RoutePath {
  static const String tabBarPage = "/";
  static const String webViewPage = "web_View";
  static const String loginUserPage = 'login_user';
  static const String loginRegisterUser = 'login_register_user';
}
相关推荐
三天不学习2 小时前
CSS 之 position 定位属性详解
前端·css·定位·position
亦可呀2 小时前
HTML-CSS-常见标签与样式
前端·css·html
web150850966413 小时前
【MsSQL】数据库基础 & 库的基本操作
前端·数据库·sqlserver
纳尼亚awsl4 小时前
处理元素卡在视野边界,滚动到视野内
前端·javascript·vue.js
黑客Jack4 小时前
XSS Challenges
前端·javascript·xss
黑客-秋凌4 小时前
XSS讲解
前端·xss
永远不会太晚4 小时前
JavaScript的diff库详解(示例:vue项目实现两段字符串比对标黄功能)
前端·javascript·vue.js
Json____4 小时前
网页单机版五子棋小游戏项目练习-初学前端可用于练习~
前端·javascript·css·html·五子棋·网页五子棋单机小程序
lecepin5 小时前
前端技术月刊-2025.1
前端·javascript·面试
sunly_6 小时前
Flutter:打包apk,详细图文介绍(一)
flutter