Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The introduction

When Xiaohong used Fluro to realize route jump in the development, he found that for component development, there is another capability that routing must have: obtaining widgets from another component through routing and filling them into his own page.

Implementation method:

  1. Display dependencies. PackageA directly depends on PackageB
  2. Extend Fluro to have the ability to return widgets via a path

Extension is a key word in dart language, which can be used to expand capabilities on the basis of the original class. If you are interested, you can search the API of this key word separately. Here we can extend Fluro to return the Widget by path:

Create rex_fluro. Dart (direct copy available)

extension RexFluroExt on FluroRouter { Widget widgetForPath( {@required String path, @required BuildContext context, Map<String, List<String>> parameters}) { final AppRoute rout = this.match(path).route; if (! (rout.handler is Handler)) { return null; } final Handler handler = rout.handler as Handler; if (handler.type ! = HandlerType.route) { handler.handlerFunc(context, parameters); return null; } return handler.handlerFunc(context, parameters); }}Copy the code

Import is used where this extension capability is needed

import 'package:rex_test/src/extension/rex_fluro.dart'; / / use / / through global Fluro management tools to control the GlobalRouterManager. The router. WidgetForPath (path: '/ pathA, context: context, the parameters: [{'params':json.encode(ModuleA.tojson())}]);Copy the code

Attach the GlobalRouteManager code

abstract class GlobalRouterManager { static FluroRouter _router = FluroRouter(); static FluroRouter get router => _router; static registerRoutes(Iterable<RouteEntry> entries) { if(entries! =null && entries.isNotEmpty){ entries.forEach((element) => _router.define(element.name, handler: element.handler, transitionType: element.transitionType)); }}}Copy the code

Before use, please be sure to guarantee in the main entrance to GlobalRouterManager. The registerRoutes () is operated by registered to carry on the right.