Skip to content

Commit

Permalink
Merge branch 'main' into feat/documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nank1ro authored Jan 27, 2025
2 parents d0683b2 + e5aa47d commit b4cfd62
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 10 deletions.
6 changes: 5 additions & 1 deletion examples/auto_route/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
analyzer:
errors:
always_put_required_named_parameters_first: ignore
include: package:very_good_analysis/analysis_options.yaml

linter:
rules:
# since this is just an example, there is no need for ubiquitous documentation.
public_member_api_docs: false
public_member_api_docs: false

25 changes: 25 additions & 0 deletions examples/auto_route/lib/controllers/books/controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:auto_route_example/controllers/books/model.dart';
import 'package:disco/disco.dart';

final booksControllerProvider = Provider((context) => BooksController());

class BooksController {
final _books = List.generate(
10,
(index) => Book(
id: index,
name: 'Book $index',
description: 'Description for book $index',
),
);

/// Returns a list of all books.
List<Book> get books {
return _books;
}

/// Returns a book by its [id].
Book book(int id) {
return _books.firstWhere((book) => book.id == id);
}
}
11 changes: 11 additions & 0 deletions examples/auto_route/lib/controllers/books/model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Book {
const Book({
required this.id,
required this.name,
required this.description,
});

final int id;
final String name;
final String description;
}
12 changes: 6 additions & 6 deletions examples/auto_route/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import 'package:auto_route_example/router.dart';
import 'package:flutter/material.dart';

void main() {
runApp(const MainApp());
}

final _appRouter = AppRouter();

class MainApp extends StatelessWidget {
const MainApp({super.key});

@override
Widget build(BuildContext context) {
// TODO(nank1ro): create an example with auto_route
return const MaterialApp(
home: Scaffold(
body: Center(
child: Text('Hello World!'),
),
return MaterialApp.router(
routerConfig: _appRouter.config(
includePrefixMatches: true,
),
);
}
Expand Down
25 changes: 25 additions & 0 deletions examples/auto_route/lib/pages/book.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:auto_route/annotations.dart';
import 'package:auto_route_example/controllers/books/controller.dart';
import 'package:flutter/material.dart';

@RoutePage()
class BookPage extends StatelessWidget {
const BookPage({
super.key,
@PathParam() required this.bookId,
});

final int bookId;

@override
Widget build(BuildContext context) {
final booksController = booksControllerProvider.of(context);
final book = booksController.book(bookId);
return Scaffold(
appBar: AppBar(title: Text(book.name)),
body: Center(
child: Text(booksController.book(bookId).description),
),
);
}
}
53 changes: 53 additions & 0 deletions examples/auto_route/lib/pages/books.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:auto_route/auto_route.dart';
import 'package:auto_route_example/controllers/books/controller.dart';
import 'package:auto_route_example/router.dart';
import 'package:disco/disco.dart';
import 'package:flutter/material.dart';

@RoutePage()
class BooksWrapperPage extends StatelessWidget implements AutoRouteWrapper {
const BooksWrapperPage({super.key});
@override
Widget wrappedRoute(BuildContext context) {
return ProviderScope(
providers: [booksControllerProvider],
child: this,
);
}

@override
Widget build(BuildContext context) {
return const AutoRouter();
}
}

@RoutePage()
class BooksPage extends StatelessWidget {
const BooksPage({super.key});

@override
Widget build(BuildContext context) {
final booksController = booksControllerProvider.of(context);
final books = booksController.books;

return Scaffold(
appBar: AppBar(
title: const Text('Books'),
leading: BackButton(
onPressed: () => const HomeRoute().navigate(context),
),
),
body: ListView.separated(
itemCount: books.length,
itemBuilder: (context, index) {
final book = books[index];
return ListTile(
title: Text(book.name),
onTap: () => BookRoute(bookId: book.id).navigate(context),
);
},
separatorBuilder: (context, index) => const SizedBox(height: 8),
),
);
}
}
26 changes: 26 additions & 0 deletions examples/auto_route/lib/pages/home.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:auto_route/auto_route.dart';
import 'package:auto_route_example/router.dart';
import 'package:flutter/material.dart';

@RoutePage()
class HomePage extends StatefulWidget {
const HomePage({super.key});

@override
State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
child: const Text('Go to books'),
onPressed: () => context.router.root.navigate(const BooksRoute()),
),
),
);
}
}
26 changes: 26 additions & 0 deletions examples/auto_route/lib/router.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:auto_route/auto_route.dart';
import 'package:auto_route_example/pages/book.dart';
import 'package:auto_route_example/pages/books.dart';
import 'package:auto_route_example/pages/home.dart';
import 'package:flutter/foundation.dart';

part 'router.gr.dart';

@AutoRouterConfig()
class AppRouter extends RootStackRouter {
@override
List<AutoRoute> get routes => [
AutoRoute(
path: '/',
page: HomeRoute.page,
),
AutoRoute(
path: '/books',
page: BooksWrapperRoute.page,
children: [
AutoRoute(path: '', page: BooksRoute.page, initial: true),
AutoRoute(path: ':bookId', page: BookRoute.page),
],
),
];
}
97 changes: 97 additions & 0 deletions examples/auto_route/lib/router.gr.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND

// **************************************************************************
// AutoRouterGenerator
// **************************************************************************

// ignore_for_file: type=lint
// coverage:ignore-file

part of 'router.dart';

/// generated route for
/// [BookPage]
class BookRoute extends PageRouteInfo<BookRouteArgs> {
BookRoute({Key? key, required int bookId, List<PageRouteInfo>? children})
: super(
BookRoute.name,
args: BookRouteArgs(key: key, bookId: bookId),
rawPathParams: {'bookId': bookId},
initialChildren: children,
);

static const String name = 'BookRoute';

static PageInfo page = PageInfo(
name,
builder: (data) {
final pathParams = data.inheritedPathParams;
final args = data.argsAs<BookRouteArgs>(
orElse: () => BookRouteArgs(bookId: pathParams.getInt('bookId')),
);
return BookPage(key: args.key, bookId: args.bookId);
},
);
}

class BookRouteArgs {
const BookRouteArgs({this.key, required this.bookId});

final Key? key;

final int bookId;

@override
String toString() {
return 'BookRouteArgs{key: $key, bookId: $bookId}';
}
}

/// generated route for
/// [BooksPage]
class BooksRoute extends PageRouteInfo<void> {
const BooksRoute({List<PageRouteInfo>? children})
: super(BooksRoute.name, initialChildren: children);

static const String name = 'BooksRoute';

static PageInfo page = PageInfo(
name,
builder: (data) {
return const BooksPage();
},
);
}

/// generated route for
/// [BooksWrapperPage]
class BooksWrapperRoute extends PageRouteInfo<void> {
const BooksWrapperRoute({List<PageRouteInfo>? children})
: super(BooksWrapperRoute.name, initialChildren: children);

static const String name = 'BooksWrapperRoute';

static PageInfo page = PageInfo(
name,
builder: (data) {
return WrappedRoute(child: const BooksWrapperPage());
},
);
}

/// generated route for
/// [HomePage]
class HomeRoute extends PageRouteInfo<void> {
const HomeRoute({List<PageRouteInfo>? children})
: super(HomeRoute.name, initialChildren: children);

static const String name = 'HomeRoute';

static PageInfo page = PageInfo(
name,
builder: (data) {
return const HomePage();
},
);
}
8 changes: 5 additions & 3 deletions examples/auto_route/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: auto_route_example
description: "An example of a Flutter project combining disco and auto_route."
publish_to: 'none'
publish_to: "none"
version: 0.0.1

environment:
Expand All @@ -10,12 +10,14 @@ resolution: workspace

dependencies:
auto_route: ^9.3.0+1
disco:
path: ../../packages/disco
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter
auto_route_generator: ^9.3.1
build_runner: ^2.4.14
very_good_analysis: ^7.0.0

flutter:
Expand Down

0 comments on commit b4cfd62

Please sign in to comment.