Flutter路由fluro使用和代码浅析

https://github.com/theyakka/fluro
特点:
1、路由导航实现简单
2、路由函数处理
3、匹配通配符参数
4、内置多种过渡动画,支持自定义过渡动画

demo:
https://github.com/anymyna/flutter-examples/work_from_home

使用步骤:

1、 添加依赖

dependencies:
  flutter:
  fluro: ^1.5.1 

2、 初始化

class MyApp extends StatefulWidget {


  MyApp()  {
    final router = new Router();
    Routes.configureRoutes(router);
    Application.router = router;
  }
  // This widget is the root of your application.
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyAppState();
  }
}

应用全局配置Router

class Application {
  static Router router;

}

3、定义路由处理

//dialog
var dialogHandler = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
      return MyDialog();
    });
class Routes {
  static String root = "/";
  static String transitionPage = "/transition";
  static String homePage = "/home";

  static String sqflitePage = "/sqflite";
  static String eventBusPage = "/eventBus";
  static String fileZipPage = "/fileZip";
  static String webViewPlginPage = "/webViewPlgin";
  static String flutterWebViewPage = "/flutterWebView";
  static String providerPage = "/provider";
  static String sharedPreferences = "/sharedPreferences";
  static String flutterChannel = "/flutterChannel";
  static String urlLauncher = "/urlLauncher";
  static String dialogShow = "/dialogShow";
  static String bannerPage = "/bannerPage";


  static void configureRoutes(Router router) {
    router.notFoundHandler = Handler(
        handlerFunc: (BuildContext context, Map<String, List<String>> params) {
        });

    router.define(transitionPage, handler: transitionHandler);
    router.define(homePage, handler: homeHandler);


    router.define(sqflitePage, handler: sqfliteHandler);
    router.define(eventBusPage, handler: eventBusHandler);
    router.define(fileZipPage, handler: fileZipHandler);
    router.define(webViewPlginPage, handler: webViewPlginHandler);
    router.define(flutterWebViewPage, handler: flutterWebViewHandler);
    router.define(providerPage, handler: providerHandler);
    router.define(sharedPreferences, handler: spHandler);
    router.define(flutterChannel, handler: channelHandler);
    router.define(urlLauncher, handler: urlLauncherHandler);
    router.define(dialogShow, handler: dialogHandler);
    router.define(bannerPage, handler: bannerHandler);

  }
}

4、路由导航

          //Diaolog
            Application.router.navigateTo(context, Routes.dialogShow,
                transition: TransitionType.cupertino);

代码解析:

1、路由定义
router.define(dialogShow, handler: dialogHandler);
路径和处理函数封装成一个节点AppRoute存储在树结构的总路由表中RouteTree

router.dart




  static final appRouter = Router();

  /// The tree structure that stores the defined routes
  final RouteTree _routeTree = RouteTree();

  /// Generic handler for when a route has not been defined
  Handler notFoundHandler;

  /// Creates a [PageRoute] definition for the passed [RouteHandler]. You can optionally provide a default transition type.
  void define(String routePath,
      {@required Handler handler, TransitionType transitionType}) {
    _routeTree.addRoute(
      AppRoute(routePath, handler, transitionType: transitionType),
    );
  }

common.dart


class AppRoute {
  String route;
  dynamic handler;
  TransitionType transitionType;
  AppRoute(this.route, this.handler, {this.transitionType});
}

2、路由导航
根据路径匹配RouteMatch,使用RouteMatch中的route,进行Navigator.push(context, route);

router.dart

///
  Future navigateTo(BuildContext context, String path,
      {bool replace = false,
      bool clearStack = false,
      TransitionType transition,
      Duration transitionDuration = const Duration(milliseconds: 250),
      RouteTransitionsBuilder transitionBuilder}) {
    RouteMatch routeMatch = matchRoute(context, path,
        transitionType: transition,
        transitionsBuilder: transitionBuilder,
        transitionDuration: transitionDuration);
    Route<dynamic> route = routeMatch.route;
    Completer completer = Completer();
    Future future = completer.future;
    if (routeMatch.matchType == RouteMatchType.nonVisual) {
      completer.complete("Non visual route type.");
    } else {
      if (route == null && notFoundHandler != null) {
        route = _notFoundRoute(context, path);
      }
      if (route != null) {
        if (clearStack) {
          future =
              Navigator.pushAndRemoveUntil(context, route, (check) => false);
        } else {
          future = replace
              ? Navigator.pushReplacement(context, route)
              : Navigator.push(context, route);
        }
        completer.complete();
      } else {
        String error = "No registered route was found to handle '$path'.";
        print(error);
        completer.completeError(RouteNotFoundException(error, path));
      }
    }

    return future;
  }

routeSettings 配置路径,handler 配置为matchRoute匹配route中的handler
android 平台默认动画情况下通过MaterialPageRoute进行路由


    RouteSettings settingsToUse = routeSettings;
    if (routeSettings == null) {
      settingsToUse = RouteSettings(name: path);
    }
    AppRouteMatch match = _routeTree.matchRoute(path);
    AppRoute route = match?.route;
    Handler handler = (route != null ? route.handler : notFoundHandler);



RouteCreator creator =
        (RouteSettings routeSettings, Map<String, List<String>> parameters) {
      bool isNativeTransition = (transition == TransitionType.native ||
          transition == TransitionType.nativeModal);
      if (isNativeTransition) {
        if (Theme.of(buildContext).platform == TargetPlatform.iOS) {
          return CupertinoPageRoute<dynamic>(
              settings: routeSettings,
              fullscreenDialog: transition == TransitionType.nativeModal,
              builder: (BuildContext context) {
                return handler.handlerFunc(context, parameters);
              });
        } else {
          return MaterialPageRoute<dynamic>(
              settings: routeSettings,
              fullscreenDialog: transition == TransitionType.nativeModal,
              builder: (BuildContext context) {
                return handler.handlerFunc(context, parameters);
              });
        }
      } 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值