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
66 changes: 66 additions & 0 deletions packages/clean_framework/example/lib/demo_router.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:clean_framework_example/features/country/presentation/country_ui.dart';
import 'package:clean_framework_example/features/last_login/presentation/last_login_ui.dart';
import 'package:clean_framework_example/features/random_cat/presentation/random_cat_ui.dart';
import 'package:clean_framework_example/home_page.dart';
import 'package:clean_framework_example/routes.dart';
import 'package:clean_framework_router/clean_framework_router.dart';
import 'package:flutter/material.dart';

class DemoRouter extends AppRouter<Routes> {
@override
RouterConfiguration configureRouter() {
return RouterConfiguration(
routes: [
AppRoute(
route: Routes.home,
builder: (context, state) => HomePage(),
routes: [
AppRoute(
route: Routes.lastLogin,
builder: (context, state) => LastLoginUI(),
),
AppRoute(
route: Routes.countries,
builder: (context, state) => CountryUI(),
routes: [
AppRoute(
route: Routes.countryDetail,
builder: (context, state) {
return Scaffold(
appBar: AppBar(
title: Text(state.params['country'] ?? ''),
),
body: Center(
child: Text(state.queryParams['capital'].toString()),
),
);
},
),
],
),
AppRoute(
route: Routes.randomCat,
builder: (context, state) => RandomCatUI(),
),
],
),
],
errorBuilder: (context, state) => Page404(error: state.error),
);
}
}

class Page404 extends StatelessWidget {
const Page404({required this.error});

final Exception? error;

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(error.toString()),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:clean_framework/clean_framework_providers.dart';
import 'package:clean_framework_example/features/country/domain/country_view_model.dart';
import 'package:clean_framework_example/features/country/presentation/country_presenter.dart';
import 'package:clean_framework_example/routes.dart';
import 'package:clean_framework_router/clean_framework_router.dart';
import 'package:flutter/material.dart';

class CountryUI extends UI<CountryViewModel> {
Expand Down Expand Up @@ -77,7 +78,7 @@ class CountryUI extends UI<CountryViewModel> {
title: Text(country.name),
subtitle: Text(country.capital),
horizontalTitleGap: 0,
onTap: () => router.to(
onTap: () => context.router.go(
Routes.countryDetail,
params: {'country': country.name},
queryParams: {'capital': country.capital},
Expand Down
3 changes: 2 additions & 1 deletion packages/clean_framework/example/lib/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:clean_framework/clean_framework.dart';
import 'package:clean_framework_example/routes.dart';
import 'package:clean_framework_router/clean_framework_router.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -99,7 +100,7 @@ class _List extends StatelessWidget {
ListTile(
title: Text(title),
leading: Icon(iconData),
onTap: () => router.to(route),
onTap: () => context.router.go(route),
),
Divider(),
],
Expand Down
28 changes: 16 additions & 12 deletions packages/clean_framework/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import 'dart:developer';

import 'package:clean_framework/clean_framework.dart';
import 'package:clean_framework_example/asset_feature_provider.dart';
import 'package:clean_framework_example/demo_router.dart';
import 'package:clean_framework_example/providers.dart';
import 'package:clean_framework_example/routes.dart';
import 'package:clean_framework_router/clean_framework_router.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
Expand All @@ -28,17 +29,20 @@ class ExampleApp extends StatelessWidget {
},
child: AppProvidersContainer(
providersContext: providersContext,
child: MaterialApp.router(
routeInformationParser: router.informationParser,
routerDelegate: router.delegate,
routeInformationProvider: router.informationProvider,
theme: ThemeData(
pageTransitionsTheme: PageTransitionsTheme(
builders: {
TargetPlatform.android: ZoomPageTransitionsBuilder(),
},
),
),
child: AppRouterScope(
create: () => DemoRouter(),
builder: (context) {
return MaterialApp.router(
routerConfig: context.router.config,
theme: ThemeData(
pageTransitionsTheme: PageTransitionsTheme(
builders: {
TargetPlatform.android: ZoomPageTransitionsBuilder(),
},
),
),
);
},
),
),
);
Expand Down
76 changes: 8 additions & 68 deletions packages/clean_framework/example/lib/routes.dart
Original file line number Diff line number Diff line change
@@ -1,73 +1,13 @@
import 'package:clean_framework_example/features/country/presentation/country_ui.dart';
import 'package:clean_framework_example/features/last_login/presentation/last_login_ui.dart';
import 'package:clean_framework_example/features/random_cat/presentation/random_cat_ui.dart';
import 'package:clean_framework_example/home_page.dart';
import 'package:clean_framework_router/clean_framework_router.dart';
import 'package:flutter/material.dart';

enum Routes {
home,
lastLogin,
countries,
countryDetail,
randomCat,
}

final router = AppRouter<Routes>(
routes: [
AppRoute(
name: Routes.home,
path: '/',
builder: (context, state) => HomePage(),
routes: [
AppRoute(
name: Routes.lastLogin,
path: 'last-login',
builder: (context, state) => LastLoginUI(),
),
AppRoute(
name: Routes.countries,
path: 'countries',
builder: (context, state) => CountryUI(),
routes: [
AppRoute(
name: Routes.countryDetail,
path: ':country',
builder: (context, state) {
return Scaffold(
appBar: AppBar(
title: Text(state.getParam('country')),
),
body: Center(
child: Text(state.queryParams['capital'].toString()),
),
);
},
),
],
),
AppRoute(
name: Routes.randomCat,
path: 'random-cat',
builder: (context, state) => RandomCatUI(),
),
],
),
],
errorBuilder: (context, state) => Page404(error: state.error),
);

class Page404 extends StatelessWidget {
const Page404({Key? key, required this.error}) : super(key: key);
enum Routes with RoutesMixin {
home('/'),
lastLogin('last-login'),
countries('countries'),
countryDetail(':country'),
randomCat('random-cat');

final Exception? error;
const Routes(this.path);

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(error.toString()),
),
);
}
final String path;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:clean_framework/clean_framework.dart';
import 'package:clean_framework_example/demo_router.dart';
import 'package:clean_framework_example/features/country/presentation/country_ui.dart';
import 'package:clean_framework_example/providers.dart';
import 'package:clean_framework_example/routes.dart';
Expand All @@ -12,6 +13,8 @@ import '../../../home_page_test.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

final router = DemoRouter();

setupUITest(context: providersContext, router: router);

final gateway = countryGatewayProvider.getGateway(providersContext);
Expand Down Expand Up @@ -98,7 +101,7 @@ void main() {
child: child,
),
verify: (tester) async {
router.to(Routes.countries);
router.go(Routes.countries);
await tester.pumpAndSettle();

final listTileFinder = find.byType(ListTile);
Expand Down
18 changes: 9 additions & 9 deletions packages/clean_framework/example/test/home_page_test.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import 'package:clean_framework/clean_framework.dart';
import 'package:clean_framework/clean_framework_defaults.dart';
import 'package:clean_framework_example/demo_router.dart';
import 'package:clean_framework_example/features/country/presentation/country_ui.dart';
import 'package:clean_framework_example/features/last_login/presentation/last_login_ui.dart';
import 'package:clean_framework_example/features/random_cat/presentation/random_cat_ui.dart';
import 'package:clean_framework_example/home_page.dart';
import 'package:clean_framework_example/providers.dart';
import 'package:clean_framework_example/routes.dart';
import 'package:clean_framework_router/clean_framework_router.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
tearDown(() {
router.reset();
});

group('HomePage tests | ', () {
testWidgets(
'correct UI',
Expand Down Expand Up @@ -113,10 +110,13 @@ Widget buildWidget(Widget widget) {
child: AppProvidersContainer(
providersContext: providersContext,
onBuild: (_, __) {},
child: MaterialApp.router(
routeInformationParser: router.informationParser,
routerDelegate: router.delegate,
routeInformationProvider: router.informationProvider,
child: AppRouterScope(
create: () => DemoRouter(),
builder: (context) {
return MaterialApp.router(
routerConfig: context.router.config,
);
},
),
),
);
Expand Down
8 changes: 6 additions & 2 deletions packages/clean_framework/example/test/routes_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:clean_framework_example/demo_router.dart';
import 'package:clean_framework_example/home_page.dart';
import 'package:clean_framework_example/routes.dart';
import 'package:clean_framework_router/clean_framework_router.dart';
import 'package:flutter_test/flutter_test.dart';

import 'home_page_test.dart';
Expand All @@ -10,7 +11,10 @@ void main() {
(tester) async {
await tester.pumpWidget(buildWidget(HomePage()));

router.open('/non-existent');
final router =
AppRouterScope.of(tester.element(find.byType(HomePage))).router;

router.goLocation('/non-existent');
await tester.pumpAndSettle();

expect(find.byType(Page404), findsOneWidget);
Expand Down
11 changes: 11 additions & 0 deletions packages/clean_framework_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# Changelog
## 0.2.0-dev.2
**Nov 3, 2022**
- Added `AppRouterState` alias.

## 0.2.0-dev.1
**Nov 2, 2022 (Breaking)**
- Upgraded to `go_router` v5.
- `AppRouter` is now abstract, in order to make it extensible.
- Routes now need to be enum mixed with `RoutesMixin`.
- Added `AppRouterScope`.

## 0.2.0-dev.0
**Nov 1, 2022**
- Upgraded to `clean_framework: ^2.0.0-dev.0`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export 'src/app_route.dart';
export 'src/app_router.dart';
export 'src/app_router_base.dart' show RouterConfiguration;
export 'src/app_router_scope.dart';
58 changes: 58 additions & 0 deletions packages/clean_framework_router/lib/src/app_route.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.dart';

export 'package:go_router/go_router.dart' show ShellRoute;

/// Signature for router's `builder` and `errorBuilder` callback.
typedef RouteWidgetBuilder = Widget Function(BuildContext, GoRouterState);

/// Signature of the page builder callback for a matched AppRoute.
typedef RoutePageBuilder = Page<void> Function(BuildContext, GoRouterState);

class AppRoute extends GoRoute {
AppRoute({
required this.route,
super.builder,
super.routes,
super.redirect,
}) : super(path: route.path, name: (route as Enum).name);

AppRoute.page({
required this.route,
RoutePageBuilder? builder,
super.routes,
super.redirect,
}) : super(
path: route.path,
name: (route as Enum).name,
pageBuilder: builder,
);

AppRoute.custom({
required this.route,
RouteWidgetBuilder? builder,
RouteTransitionsBuilder? transitionsBuilder,
super.routes,
super.redirect,
}) : super(
path: route.path,
name: (route as Enum).name,
pageBuilder: builder == null
? null
: (context, state) {
final transBuilder =
transitionsBuilder ?? (_, __, ___, child) => child;

return CustomTransitionPage(
child: builder(context, state),
transitionsBuilder: transBuilder,
);
},
);

final RoutesMixin route;
}

mixin RoutesMixin {
String get path;
}
Loading