Skip to content

Commit

Permalink
Merge pull request #51 from nubank/fix-for-flutter-122
Browse files Browse the repository at this point in the history
Fixes for Flutter 1.22 Navigator 2.0
  • Loading branch information
Lucas Britto authored Oct 19, 2020
2 parents c066347 + e156c78 commit a09b4cb
Show file tree
Hide file tree
Showing 18 changed files with 58 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 0.7.0
- [BREAKING] Rename classes to avoid conflicts with Flutter 1.22 Router Widget
- `Router` was renamed to `NuRouter`
- The `NuRouter` annotation was renamed to `nuRouter` const or `customNuRouter` function

## 0.6.2
- Fix NPE exception when a old instance of router tries calling some methods on it's nuvigator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import 'package:nuvigator/nuvigator.dart';

part 'composer_routes.g.dart';

@NuRouter()
class ComposerRouter extends Router {
@nuRouter
class ComposerRouter extends NuRouter {
@override
String get deepLinkPrefix => '/composer';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class _TextComposerScreenState extends State<TextComposerScreen> {
tag: 'HERO',
child: FlutterLogo(
size: 48,
colors: Colors.deepPurple,
),
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import '../../composer/navigation/composer_routes.dart';

part 'friend_request_router.g.dart';

@NuRouter()
class FriendRequestRouter extends Router {
@nuRouter
class FriendRequestRouter extends NuRouter {
FriendRequestRouter();

@NuRoute()
Expand All @@ -23,7 +23,7 @@ class FriendRequestRouter extends Router {
ScreenRoute<void> success() => ScreenRoute(
builder: (context) => SuccessScreen(
closeFlow: () => nuvigator.closeFlow(),
toComposeText: () => Router.of<SamplesRouter>(context)
toComposeText: () => NuRouter.of<SamplesRouter>(context)
.composerRouter
.pushReplacementToComposeText(),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class SuccessScreen extends StatelessWidget {
tag: 'HERO',
child: FlutterLogo(
size: 96,
colors: Colors.deepPurple,
),
),
Text(
Expand Down
10 changes: 5 additions & 5 deletions example/lib/samples/navigation/samples_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import '../bloc/samples_bloc.dart';

part 'samples_router.g.dart';

@NuRouter()
class SamplesRouter extends Router {
@nuRouter
class SamplesRouter extends NuRouter {
@override
String get deepLinkPrefix => 'deepprefix';

Expand All @@ -34,8 +34,8 @@ class SamplesRouter extends Router {
),
);

@NuRouter()
final composerRouter = ComposerRouter();
@nuRouter
final ComposerRouter composerRouter = ComposerRouter();

@override
WrapperFn get screensWrapper => (BuildContext context, Widget child) {
Expand All @@ -49,5 +49,5 @@ class SamplesRouter extends Router {
Map<RouteDef, ScreenRouteBuilder> get screensMap => _$screensMap;

@override
List<Router> get routers => _$routers;
List<NuRouter> get routers => _$routers;
}
2 changes: 1 addition & 1 deletion example/lib/samples/navigation/samples_router.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions example/lib/samples/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final bloc = Provider.of<SamplesBloc>(context);
final router = Router.of<SamplesRouter>(context);
final router = NuRouter.of<SamplesRouter>(context);
final headingStle = Theme.of(context).textTheme.headline3;
final toggleStyle = Theme.of(context).textTheme.bodyText1;

Expand All @@ -27,7 +27,6 @@ class HomeScreen extends StatelessWidget {
const Hero(
tag: 'HERO',
child: FlutterLogo(
colors: Colors.deepPurple,
size: 96,
),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:source_gen/source_gen.dart';

import 'builder/builder_library.dart';

class NuvigatorGenerator extends GeneratorForAnnotation<NuRouter> {
class NuvigatorGenerator extends GeneratorForAnnotation<NuRouterAnnotation> {
@override
FutureOr<String> generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) {
Expand Down
2 changes: 1 addition & 1 deletion lib/builder/builder_library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BuilderLibrary extends BaseBuilder {
(b) => b
..name = '_\$routers'
..type = MethodType.getter
..returns = refer('List<Router>')
..returns = refer('List<NuRouter>')
..lambda = true
..body = Code('[$code]'),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/builder/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ List<DartType> getGenericTypes(DartType type) {
}

const nuRouteChecker = TypeChecker.fromRuntime(NuRoute);
const nuRouterChecker = TypeChecker.fromRuntime(NuRouter);
const nuRouterChecker = TypeChecker.fromRuntime(NuRouterAnnotation);

String capitalize(String s) => s[0].toUpperCase() + s.substring(1);

Expand Down
2 changes: 1 addition & 1 deletion lib/nuvigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ library nuvigator;

export 'src/annotations.dart';
export 'src/helpers.dart';
export 'src/nurouter.dart';
export 'src/nuvigator.dart';
export 'src/router.dart';
export 'src/screen_route.dart';
export 'src/screen_type.dart';
export 'src/screen_types/cupertino_screen_type.dart';
Expand Down
11 changes: 9 additions & 2 deletions lib/src/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,20 @@
///
/// Obs: The [routeNamePrefix] is only used by the routes class to compose the
/// route names.
class NuRouter {
const NuRouter({this.routerName, this.routeNamePrefix});
class NuRouterAnnotation {
const NuRouterAnnotation({this.routerName, this.routeNamePrefix});

final String routerName;
final String routeNamePrefix;
}

NuRouterAnnotation customNuRouter(
{String routeNamePrefix, String routerName}) =>
NuRouterAnnotation(
routeNamePrefix: routeNamePrefix, routerName: routerName);

const nuRouter = NuRouterAnnotation();

/// Annotation used to define a route.
///
/// You can set a [routeName], [deepLink] and [pushMethods].
Expand Down
14 changes: 7 additions & 7 deletions lib/src/router.dart → lib/src/nurouter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ typedef ScreenRouteBuilder = ScreenRoute Function(RouteSettings settings);

String deepLinkString(Uri url) => url.host + url.path;

typedef HandleDeepLinkFn = Future<dynamic> Function(Router router, Uri uri,
typedef HandleDeepLinkFn = Future<dynamic> Function(NuRouter router, Uri uri,
[bool isFromNative, dynamic args]);

Map<String, String> extractDeepLinkParameters(
Expand Down Expand Up @@ -38,8 +38,8 @@ class RouteEntry {
bool operator ==(Object other) => other is RouteEntry && other.key == key;
}

abstract class Router {
static T of<T extends Router>(
abstract class NuRouter {
static T of<T extends NuRouter>(
BuildContext context, {
bool nullOk = false,
bool rootRouter = false,
Expand Down Expand Up @@ -73,14 +73,14 @@ abstract class Router {

WrapperFn get screensWrapper => null;

List<Router> get routers => [];
List<NuRouter> get routers => [];

Map<RouteDef, ScreenRouteBuilder> get screensMap => {};

String get deepLinkPrefix => '';

/// Get the specified router that can be grouped in this router
T getRouter<T extends Router>() {
T getRouter<T extends NuRouter>() {
if (this is T) return this;
for (final router in routers) {
final r = router.getRouter<T>();
Expand All @@ -93,7 +93,7 @@ abstract class Router {
final screen = _getScreen(settings);
if (screen != null) return screen;

for (Router router in routers) {
for (NuRouter router in routers) {
final screen = router.getScreen(settings);
if (screen != null) return screen.wrapWith(screensWrapper);
}
Expand All @@ -107,7 +107,7 @@ abstract class Router {
if (prefixRegex.hasMatch(deepLink)) {
final routeEntry = _getRouteEntryForDeepLink(deepLink);
if (routeEntry != null) return routeEntry;
for (final Router router in routers) {
for (final NuRouter router in routers) {
final newDeepLink = deepLink.replaceFirst(thisDeepLinkPrefix, '');
final subRouterEntry = router.getRouteEntryForDeepLink(newDeepLink);
if (subRouterEntry != null) {
Expand Down
16 changes: 8 additions & 8 deletions lib/src/nuvigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:nuvigator/nuvigator.dart';

import 'router.dart';
import 'nurouter.dart';

typedef ObserverBuilder = NavigatorObserver Function();

NuvigatorState _tryToFindNuvigatorForRouter<T extends Router>(
NuvigatorState _tryToFindNuvigatorForRouter<T extends NuRouter>(
NuvigatorState nuvigatorState) {
if (nuvigatorState == null) return null;
final nuvigatorRouterForType = nuvigatorState.router.getRouter<T>();
Expand Down Expand Up @@ -52,7 +52,7 @@ class NuvigatorStateTracker extends NavigatorObserver {
}
}

class Nuvigator<T extends Router> extends Navigator {
class Nuvigator<T extends NuRouter> extends Navigator {
Nuvigator({
@required this.router,
String initialRoute,
Expand Down Expand Up @@ -142,13 +142,13 @@ class Nuvigator<T extends Router> extends Navigator {
return this;
}

static NuvigatorState ofRouter<T extends Router>(BuildContext context) {
static NuvigatorState ofRouter<T extends NuRouter>(BuildContext context) {
final NuvigatorState closestNuvigator =
context.findAncestorStateOfType<NuvigatorState>();
return _tryToFindNuvigatorForRouter<T>(closestNuvigator);
}

static NuvigatorState<T> of<T extends Router>(
static NuvigatorState<T> of<T extends NuRouter>(
BuildContext context, {
bool rootNuvigator = false,
bool nullOk = false,
Expand Down Expand Up @@ -176,7 +176,7 @@ class Nuvigator<T extends Router> extends Navigator {
}
}

class NuvigatorState<T extends Router> extends NavigatorState
class NuvigatorState<T extends NuRouter> extends NavigatorState
with WidgetsBindingObserver {
NuvigatorState get rootNuvigator =>
Nuvigator.of(context, rootNuvigator: true) ?? this;
Expand All @@ -190,7 +190,7 @@ class NuvigatorState<T extends Router> extends NavigatorState

NuvigatorStateTracker stateTracker;

R getRouter<R extends Router>() => router.getRouter<R>();
R getRouter<R extends NuRouter>() => router.getRouter<R>();

List<ObserverBuilder> _collectObservers() {
if (isNested) {
Expand Down Expand Up @@ -343,7 +343,7 @@ class NuvigatorState<T extends Router> extends NavigatorState

bool get isRoot => this == rootNuvigator;

Router get rootRouter => rootNuvigator.router;
NuRouter get rootRouter => rootNuvigator.router;

@override
Widget build(BuildContext context) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: nuvigator
description: A powerful and strongly typed routing abstraction over Flutter navigator, providing some new features and an easy way to define routers with code generation.
version: 0.6.2
version: 0.7.0

homepage: https://github.com/nubank/nuvigator

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/test_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:nuvigator/nuvigator.dart';

part 'test_router.g.dart';

@NuRouter()
class TestRouter extends Router {
@nuRouter
class TestRouter extends NuRouter {
@NuRoute(deepLink: 'home')
ScreenRoute<void> home() => ScreenRoute(
builder: (context) => const Text('Home'),
Expand Down
19 changes: 10 additions & 9 deletions test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:nuvigator/nuvigator.dart';

class TestRouter extends Router {
class TestRouter extends NuRouter {
@override
Map<RouteDef, ScreenRouteBuilder> get screensMap => {
RouteDef('firstScreen', deepLink: 'test/simple'): (_) => ScreenRoute(
Expand All @@ -20,7 +20,7 @@ class TestRouter extends Router {
};
}

class TestRouterWPrefix extends Router {
class TestRouterWPrefix extends NuRouter {
@override
String get deepLinkPrefix => 'prefix/';

Expand All @@ -40,7 +40,7 @@ class TestRouterWPrefix extends Router {
};
}

class TestRouterWWrapper extends Router {
class TestRouterWWrapper extends NuRouter {
@override
Map<RouteDef, ScreenRouteBuilder> get screensMap => {
RouteDef('firstScreen', deepLink: 'test/simple'): (_) => ScreenRoute(
Expand All @@ -57,14 +57,14 @@ class TestRouterWWrapper extends Router {
};
}

class GroupTestRouter extends Router {
class GroupTestRouter extends NuRouter {
@override
String get deepLinkPrefix => 'group/';

TestRouter testRouter = TestRouter();

@override
List<Router> get routers => [
List<NuRouter> get routers => [
testRouter,
];

Expand All @@ -85,10 +85,10 @@ class MockNuvigator extends NuvigatorState {
String routePushed;
Object argumentsPushed;
@override
final Router router;
final NuRouter router;

@override
NuvigatorState<Router> get rootNuvigator => this;
NuvigatorState<NuRouter> get rootNuvigator => this;

@override
Future<T> pushNamed<T extends Object>(String routeName,
Expand All @@ -107,7 +107,7 @@ class TestWidget extends StatelessWidget {
}
}

Widget testApp(Router router, String initialRoute, [WrapperFn wrapper]) {
Widget testApp(NuRouter router, String initialRoute, [WrapperFn wrapper]) {
return MaterialApp(
builder: Nuvigator(
router: router,
Expand All @@ -117,7 +117,8 @@ Widget testApp(Router router, String initialRoute, [WrapperFn wrapper]) {
);
}

Future pumpApp(WidgetTester tester, Router router, String initialRoute) async {
Future pumpApp(
WidgetTester tester, NuRouter router, String initialRoute) async {
await tester.pumpWidget(MaterialApp(
title: 'Test Nuvigator',
builder: Nuvigator(
Expand Down

0 comments on commit a09b4cb

Please sign in to comment.