Skip to content

Commit

Permalink
fix: make EmptyShellRoute() a const
Browse files Browse the repository at this point in the history
chores: add nested navigation example
chores: add declarative navigation example
  • Loading branch information
Milad-Akarie committed Jul 31, 2024
1 parent 211e963 commit d3ff303
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 26 deletions.
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,20 @@ AutoRoute(
)
```

or by using a `RedirectRoute`

```dart
AutoRoute(
path: '/dashboard',
page: DashboardRoute.page,
children: [
RedirectRoute(path: '', redirectTo: 'users'),
AutoRoute(path: 'users', page: UsersRoute.page),
AutoRoute(path: 'posts', page: PostsRoute.page),
],
)
```

#### Creating Empty Shell routes
Empty shell routes build a screen that contain the `AutoRouter` widget, which is used to render nested routes.
So you can build the widget your self like follows:
Expand All @@ -440,24 +454,12 @@ class MyShellPage extends AutoRouter {
finally you can create a shell route without code generation using the `EmptyShellRoute` helper

```dart
final BooksTab = EmptyShellRoute('BooksTab');
const BooksTab = EmptyShellRoute('BooksTab');
context.push(BooksTab());
```


or by using a `RedirectRoute`

```dart
AutoRoute(
path: '/dashboard',
page: DashboardRoute.page,
children: [
RedirectRoute(path: '', redirectTo: 'users'),
AutoRoute(path: 'users', page: UsersRoute.page),
AutoRoute(path: 'posts', page: PostsRoute.page),
],
)
```

### Things to keep in mind when implementing nested navigation

Expand Down Expand Up @@ -1539,9 +1541,10 @@ void initState(){


## Examples

- [Declarative Navigation](https://github.com/Milad-Akarie/auto_route_library/blob/master/auto_route/example/lib/declarative/declarative.router.dart)

- [Nested Navigation](https://github.com/Milad-Akarie/auto_route_library/blob/master/auto_route/example/lib/nested-navigation/nested_navigation.router.dart)

### Support auto_route

You can support auto_route by liking it on Pub and staring it on Github, sharing ideas on how we can enhance a certain functionality or by reporting any problems you encounter and of course buying a couple coffees will help speed up the development process
4 changes: 2 additions & 2 deletions auto_route/example/lib/mobile/router/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ class AppRouter extends RootStackRouter {
];
}

final BooksTab = EmptyShellRoute('BooksTab');
final ProfileTab = EmptyShellRoute('ProfileTab');
const BooksTab = EmptyShellRoute('BooksTab');
const ProfileTab = EmptyShellRoute('ProfileTab');
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'package:auto_route/auto_route.dart';
import 'package:example/nested-navigation/nested_navigation.router.gr.dart';
import 'package:flutter/material.dart';

void main() {
runApp(NestedNavigationApp());
}

class NestedNavigationApp extends StatelessWidget {
NestedNavigationApp({super.key});

final nestedRouter = NestedRouter();

@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: nestedRouter.config(),
);
}
}

@AutoRouterConfig(generateForDir: ['lib/nested-navigation'])
class NestedRouter extends RootStackRouter {
@override
List<AutoRoute> get routes => [
AutoRoute(
initial: true,
page: HostRoute.page,
children: [
AutoRoute(page: FirstRoute.page, initial: true),
AutoRoute(page: SecondRoute.page),
],
),
];
}

@RoutePage()
class HostScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Host Screen'),
/// This will automatically display a back button if the nested router can pop
leading: AutoLeadingButton(),
),
body: AutoRouter(),
);
}
}

@RoutePage()
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => context.pushRoute(SecondRoute()),
child: Text('Go to second screen'),
),
),
);
}
}

@RoutePage()
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => context.maybePop(),
child: Text('Go Back'),
),
),
);
}
}

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

12 changes: 10 additions & 2 deletions auto_route/lib/src/route/page_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ class PageInfo {
final String name;

/// The builder function of the generated [RoutePage]
final Widget Function(RouteData data) builder;
final AutoRoutePageBuilder builder;

/// Default constructor
const PageInfo(this.name, {required this.builder});
const PageInfo(this.name, {required this.builder}) ;

/// Builds an empty shell [PageInfo] with a const builder
const PageInfo.emptyShell(this.name) : builder = _emptyShellBuilder;


static Widget _emptyShellBuilder(RouteData _){
return const AutoRouter();
}

@override
bool operator ==(Object other) =>
Expand Down
13 changes: 6 additions & 7 deletions auto_route/lib/src/route/page_route_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,17 @@ class PageRouteInfo<T> {

/// A proxy Route page that provides a way to create a [PageRouteInfo]
/// without the need for creating a new Page Widget
class EmptyShellRoute {
/// Page name
final String name;
class EmptyShellRoute extends PageInfo {

/// Default constructor
EmptyShellRoute(this.name);

/// creates [PageInfo] with an just an [AutoRouter] widget
late final page = PageInfo(name, builder: (_) => const AutoRouter());
const EmptyShellRoute(super.name) : super.emptyShell();

/// Creates a new instance with of [PageRouteInfo]
PageRouteInfo call({List<PageRouteInfo>? children}) {
return PageRouteInfo(name, initialChildren: children);
}

/// Creates a new instance with of [PageInfo] with an empty shell builder
/// that returns an [AutoRouter] widget
PageInfo get page => this;
}

0 comments on commit d3ff303

Please sign in to comment.