Skip to content
Merged
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
1 change: 1 addition & 0 deletions example/lib/app/app_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class AppWidget extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: "/",
navigatorKey: Modular.navigatorKey,
onGenerateRoute: Modular.generateRoute,
);
}
Expand Down
23 changes: 23 additions & 0 deletions example/lib/app/modules/home/guard/guard.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import 'package:flutter_modular/flutter_modular.dart';

class LoginExecutor extends GuardExecutor {

final String message;
LoginExecutor({this.message});

@override
onGuarded(String path, bool isActive) {
if(isActive) {
print('logined and pass');
return;
}

print('toast: need login => $message');

// Suppose login.
Modular.to.pushNamed('/list/10');
}
}

class MyGuard implements RouteGuard {
@override
bool canActivate(String url) {
return url != '/list/2';
}

@override
// TODO: implement executors
List<GuardExecutor> get executors => [LoginExecutor(message: 'List page')];
}
15 changes: 14 additions & 1 deletion example/lib/app/modules/home/home_module.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import 'package:example/app/modules/home/pages/list/list_widget.dart';
import 'package:flutter_modular/flutter_modular.dart';

import 'package:flutter/material.dart';
import 'guard/guard.dart';
import 'home_bloc.dart';
import 'home_widget.dart';

class SlowerPageRoute extends MaterialPageRoute {
@override
// TODO: implement transitionDuration
Duration get transitionDuration => Duration(milliseconds: 1200);

Map eventP;
SlowerPageRoute({
@required builder,
@required settings,
}) : super(builder: builder, settings: settings);
}

class HomeModule extends ChildModule {
@override
List<Bind> get binds => [
Expand All @@ -19,6 +31,7 @@ class HomeModule extends ChildModule {
),
Router(
"/list/:id",
routeGenerator: (b, s) => SlowerPageRoute(builder: b, settings: s),
child: (_, args) => ListWidget(
param: int.parse(args.params['id']),
),
Expand Down
6 changes: 6 additions & 0 deletions lib/src/interfaces/route_guard.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

abstract class GuardExecutor {
onGuarded(String path, bool isActive);
}

abstract class RouteGuard {
bool canActivate(String url);

List<GuardExecutor> get executors;
}
25 changes: 12 additions & 13 deletions lib/src/modular_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,17 @@ class Modular {

static RouteGuard _verifyGuard(List<RouteGuard> guards, String path) {
RouteGuard guard;
try {
guard = guards.length == 0
? null
: guards.firstWhere((guard) => !guard.canActivate(path),
orElse: null);
} catch (e) {}
var realGuards = guards ?? [];
guard = realGuards.length == 0
? null
: guards.firstWhere((guard) => !guard.canActivate(path),
orElse: () => null);

realGuards.expand((c) => c.executors).forEach((c) => c.onGuarded(path, guard == null));

if(guard != null) {
throw ModularError("Path guarded : $path");
}
return guard;
}

Expand Down Expand Up @@ -303,6 +307,7 @@ class Modular {

if (router.transition == TransitionType.defaultTransition) {
router = router.copyWith(
routeGenerator: route.routeGenerator,
transition: route.transition,
);
}
Expand All @@ -313,13 +318,7 @@ class Modular {
if (searchRoute(route, tempRouteName, path)) {
var guards = _prepareGuardList(_masterRouteGuards, route.guards);
_masterRouteGuards = null;
RouteGuard guard;
try {
guard = guards.length == 0
? null
: guards.firstWhere((guard) => !guard.canActivate(path),
orElse: null);
} catch (e) {}
RouteGuard guard = _verifyGuard(guards, path);
if ((tempRouteName == path || tempRouteName == "$path/") &&
path != '/') {
guard = _verifyGuard(guards, path);
Expand Down
14 changes: 12 additions & 2 deletions lib/src/routers/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:flutter_modular/src/interfaces/child_module.dart';
import 'package:flutter_modular/src/interfaces/route_guard.dart';
import 'package:flutter_modular/src/transitions/transitions.dart';

typedef RouteBuilder<T> = MaterialPageRoute<T> Function(WidgetBuilder, RouteSettings);

_debugPrintModular(String text) {
if (Modular.debugMode) {
debugPrint(text);
Expand All @@ -19,6 +21,7 @@ class Router<T> {
final List<RouteGuard> guards;
final TransitionType transition;
final CustomTransition customTransition;
final RouteBuilder<T> routeGenerator;

Router(
this.routerName, {
Expand All @@ -27,6 +30,7 @@ class Router<T> {
this.guards,
this.params,
this.transition = TransitionType.defaultTransition,
this.routeGenerator,
this.customTransition,
}) {
assert(routerName != null);
Expand Down Expand Up @@ -64,13 +68,15 @@ class Router<T> {
Map<String, String> params,
List<RouteGuard> guards,
TransitionType transition,
RouteBuilder routeGenerator,
CustomTransition customTransition}) {
return Router<T>(
routerName ?? this.routerName,
child: child ?? this.child,
module: module ?? this.module,
params: params ?? this.params,
guards: guards ?? this.guards,
routeGenerator: routeGenerator ?? this.routeGenerator,
transition: transition ?? this.transition,
customTransition: customTransition ?? this.customTransition,
);
Expand Down Expand Up @@ -120,10 +126,14 @@ class Router<T> {
transitionDuration: this.customTransition.transitionDuration,
);
} else if (this.transition == TransitionType.defaultTransition) {
var widgetBuilder = (context) => _disposableGenerate(context,
args: arguments, injectMap: injectMap, path: settings.name);
if(routeGenerator != null) {
return routeGenerator(widgetBuilder, settings);
}
return MaterialPageRoute<T>(
settings: settings,
builder: (context) => _disposableGenerate(context,
args: arguments, injectMap: injectMap, path: settings.name),
builder: widgetBuilder,
);
} else {
var selectTransition = _transitions[this.transition];
Expand Down
3 changes: 3 additions & 0 deletions test/app/guard/guard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ class MyGuard implements RouteGuard {
bool canActivate(String url) {
return false;
}

@override
List<GuardExecutor> get executors => [];
}
8 changes: 4 additions & 4 deletions test/modular_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ void main() {
});

test('RouteGuard test', () {
expect(Modular.selectRoute("/forbidden"), null);
expect(() => Modular.selectRoute("/forbidden"), throwsA(isA<ModularError>()));
});
test('RouteGuard other module', () {
expect(Modular.selectRoute("/home/forbidden2"), null);
expect(() => Modular.selectRoute("/home/forbidden2"), throwsA(isA<ModularError>()));
});
test('RouteGuard other module', () {
expect(Modular.selectRoute("/home/forbidden2"), null);
expect(() => Modular.selectRoute("/home/forbidden2"), throwsA(isA<ModularError>()));
});

test('RouteGuard other module Two', () {
expect(Modular.selectRoute("/homeTwo/forbidden2"), null);
expect(() => Modular.selectRoute("/homeTwo/forbidden2"), throwsA(isA<ModularError>()));
});

test('Get route correct', () {
Expand Down