From d71003fa128e1bb035df446d68e0fe43c79aa6a7 Mon Sep 17 00:00:00 2001 From: Joe Chan Date: Tue, 19 Nov 2024 09:37:57 +0800 Subject: [PATCH] change routeFactory to routeMap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将初始化的routeFactory方法改为routeMap --- example/lib/main.dart | 10 +------ lib/src/boost_navigator.dart | 48 ++++++++++++++++++++++++++++------ lib/src/flutter_boost_app.dart | 19 +++++++++++++- 3 files changed, 59 insertions(+), 18 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 84be6be4..8b3324a6 100755 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -427,14 +427,6 @@ class _MyAppState extends State { }, }; - Route? routeFactory(RouteSettings settings, String? uniqueId) { - FlutterBoostRouteFactory? func = routerMap[settings.name!]; - if (func == null) { - return null; - } - return func(settings, uniqueId); - } - @override void initState() { super.initState(); @@ -442,7 +434,7 @@ class _MyAppState extends State { @override Widget build(BuildContext context) { - return FlutterBoostApp(routeFactory, + return FlutterBoostApp.routeMap(routerMap, // 如果自定了appBuilder,需要将传入的参数添加到widget层次结构中去, // 否则会导致FluttBoost初始化失败。 appBuilder: (child) => MaterialApp( diff --git a/lib/src/boost_navigator.dart b/lib/src/boost_navigator.dart index 35a7e2cd..4cffbd51 100644 --- a/lib/src/boost_navigator.dart +++ b/lib/src/boost_navigator.dart @@ -14,14 +14,18 @@ typedef FlutterBoostRouteFactory = Route? Function( RouteSettings settings, String? uniqueId); FlutterBoostRouteFactory routeFactoryWrapper( - FlutterBoostRouteFactory routeFactory) { + FlutterBoostRouteFactory? routeFactory) { return (settings, uniqueId) { - var route = routeFactory(settings, uniqueId); + Route? route; + if (routeFactory != null) { + route = routeFactory(settings, uniqueId); + } if (route == null && settings.name == '/') { route = PageRouteBuilder( settings: settings, pageBuilder: (_, __, ___) => Container()); } - return route; + assert(route != null, 'unable to build route(${settings.name})'); + return route!; }; } @@ -36,13 +40,37 @@ class BoostNavigator { /// The boost data center FlutterBoostAppState? appState; + Map? _routeMap; + + set routerMap(Map map) { + _routeMap = map; + //add '/' root-route if _routeMap doesn't contain. + if (!_routeMap!.containsKey('/')) { + _routeMap!['/'] = (settings, uniqueId) { + return PageRouteBuilder( + settings: settings, pageBuilder: (_, __, ___) => Container()); + }; + } + } + /// The route table in flutter_boost - late FlutterBoostRouteFactory _routeFactory; + FlutterBoostRouteFactory? _routeFactory; - set routeFactory(FlutterBoostRouteFactory routeFactory) => + @Deprecated(('Use `routerMap` instead')) + set routeFactory(FlutterBoostRouteFactory? routeFactory) => _routeFactory = routeFactoryWrapper(routeFactory); - FlutterBoostRouteFactory get routeFactory => _routeFactory; + FlutterBoostRouteFactory? get routeFactory => _routeFactory; + + Route? buildRoute(RouteSettings settings, String? uniqueId) { + if (_routeMap != null && _routeMap!.isNotEmpty) { + var factory = _routeMap![settings.name]; + if (factory != null) { + return factory(settings, uniqueId); + } + } + return routeFactory!(settings, uniqueId); + } /// Use BoostNavigator.instance instead @Deprecated('Use `instance` instead.') @@ -59,8 +87,12 @@ class BoostNavigator { /// /// If the name of route can be found in route table then return true, /// otherwise return false. - bool isFlutterPage(String name) => - routeFactory(RouteSettings(name: name), null) != null; + bool isFlutterPage(String name) { + if (_routeMap != null && _routeMap!.isNotEmpty) { + return _routeMap!.containsKey(name); + } + return routeFactory!(RouteSettings(name: name), null) != null; + } /// Push the page with the given [name] onto the hybrid stack. /// [arguments] is the param you want to pass in next page diff --git a/lib/src/flutter_boost_app.dart b/lib/src/flutter_boost_app.dart index ef83ff59..73632ed7 100644 --- a/lib/src/flutter_boost_app.dart +++ b/lib/src/flutter_boost_app.dart @@ -23,6 +23,23 @@ import 'messages.dart'; typedef FlutterBoostAppBuilder = Widget Function(Widget home); class FlutterBoostApp extends StatefulWidget { + + FlutterBoostApp.routeMap( + Map routerMap, { + Key? key, + FlutterBoostAppBuilder? appBuilder, + String? initialRoute, + + ///interceptors is to intercept push operation now + List? interceptors, + }) : appBuilder = appBuilder ?? _defaultAppBuilder, + interceptors = interceptors ?? [], + initialRoute = initialRoute ?? '/', + super(key: key) { + BoostNavigator.instance.routerMap = routerMap; + } + + @Deprecated('Use [FlutterBoostApp.routeMap] instead.') FlutterBoostApp( FlutterBoostRouteFactory routeFactory, { Key? key, @@ -743,7 +760,7 @@ class BoostPage extends Page { BoostPage._({LocalKey? key, required this.pageInfo}) : super( key: key, name: pageInfo.pageName, arguments: pageInfo.arguments) { - _route = BoostNavigator.instance.routeFactory(this, pageInfo.uniqueId) + _route = BoostNavigator.instance.buildRoute(this, pageInfo.uniqueId) as Route?; assert(_route != null, "Oops! Route name is not registered: '${pageInfo.pageName}'.");