Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change routeFactory to routeMap #2152

Open
wants to merge 1 commit into
base: dev/exp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -427,22 +427,14 @@ class _MyAppState extends State<MyApp> {
},
};

Route<dynamic>? routeFactory(RouteSettings settings, String? uniqueId) {
FlutterBoostRouteFactory? func = routerMap[settings.name!];
if (func == null) {
return null;
}
return func(settings, uniqueId);
}

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return FlutterBoostApp(routeFactory,
return FlutterBoostApp.routeMap(routerMap,
// 如果自定了appBuilder,需要将传入的参数添加到widget层次结构中去,
// 否则会导致FluttBoost初始化失败。
appBuilder: (child) => MaterialApp(
Expand Down
48 changes: 40 additions & 8 deletions lib/src/boost_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ typedef FlutterBoostRouteFactory = Route<dynamic>? Function(
RouteSettings settings, String? uniqueId);

FlutterBoostRouteFactory routeFactoryWrapper(
FlutterBoostRouteFactory routeFactory) {
FlutterBoostRouteFactory? routeFactory) {
return (settings, uniqueId) {
var route = routeFactory(settings, uniqueId);
Route<dynamic>? route;
if (routeFactory != null) {
route = routeFactory(settings, uniqueId);
}
if (route == null && settings.name == '/') {
route = PageRouteBuilder<dynamic>(
settings: settings, pageBuilder: (_, __, ___) => Container());
}
return route;
assert(route != null, 'unable to build route(${settings.name})');
return route!;
};
}

Expand All @@ -36,13 +40,37 @@ class BoostNavigator {
/// The boost data center
FlutterBoostAppState? appState;

Map<String, FlutterBoostRouteFactory>? _routeMap;

set routerMap(Map<String, FlutterBoostRouteFactory> map) {
_routeMap = map;
//add '/' root-route if _routeMap doesn't contain.
if (!_routeMap!.containsKey('/')) {
_routeMap!['/'] = (settings, uniqueId) {
return PageRouteBuilder<dynamic>(
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<dynamic>? 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.')
Expand All @@ -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
Expand Down
19 changes: 18 additions & 1 deletion lib/src/flutter_boost_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ import 'messages.dart';
typedef FlutterBoostAppBuilder = Widget Function(Widget home);

class FlutterBoostApp extends StatefulWidget {

FlutterBoostApp.routeMap(
Map<String, FlutterBoostRouteFactory> routerMap, {
Key? key,
FlutterBoostAppBuilder? appBuilder,
String? initialRoute,

///interceptors is to intercept push operation now
List<BoostInterceptor>? interceptors,
}) : appBuilder = appBuilder ?? _defaultAppBuilder,
interceptors = interceptors ?? <BoostInterceptor>[],
initialRoute = initialRoute ?? '/',
super(key: key) {
BoostNavigator.instance.routerMap = routerMap;
}

@Deprecated('Use [FlutterBoostApp.routeMap] instead.')
FlutterBoostApp(
FlutterBoostRouteFactory routeFactory, {
Key? key,
Expand Down Expand Up @@ -743,7 +760,7 @@ class BoostPage<T> extends Page<T> {
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<T>?;
assert(_route != null,
"Oops! Route name is not registered: '${pageInfo.pageName}'.");
Expand Down