diff --git a/examples/auto_route/analysis_options.yaml b/examples/auto_route/analysis_options.yaml index 66f6445..319aa2f 100644 --- a/examples/auto_route/analysis_options.yaml +++ b/examples/auto_route/analysis_options.yaml @@ -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 \ No newline at end of file + public_member_api_docs: false + diff --git a/examples/auto_route/lib/controllers/books/controller.dart b/examples/auto_route/lib/controllers/books/controller.dart new file mode 100644 index 0000000..a1c4fa1 --- /dev/null +++ b/examples/auto_route/lib/controllers/books/controller.dart @@ -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 get books { + return _books; + } + + /// Returns a book by its [id]. + Book book(int id) { + return _books.firstWhere((book) => book.id == id); + } +} diff --git a/examples/auto_route/lib/controllers/books/model.dart b/examples/auto_route/lib/controllers/books/model.dart new file mode 100644 index 0000000..5ab8a28 --- /dev/null +++ b/examples/auto_route/lib/controllers/books/model.dart @@ -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; +} diff --git a/examples/auto_route/lib/main.dart b/examples/auto_route/lib/main.dart index 3b59221..d0d3752 100644 --- a/examples/auto_route/lib/main.dart +++ b/examples/auto_route/lib/main.dart @@ -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, ), ); } diff --git a/examples/auto_route/lib/pages/book.dart b/examples/auto_route/lib/pages/book.dart new file mode 100644 index 0000000..af17091 --- /dev/null +++ b/examples/auto_route/lib/pages/book.dart @@ -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), + ), + ); + } +} diff --git a/examples/auto_route/lib/pages/books.dart b/examples/auto_route/lib/pages/books.dart new file mode 100644 index 0000000..fae696d --- /dev/null +++ b/examples/auto_route/lib/pages/books.dart @@ -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), + ), + ); + } +} diff --git a/examples/auto_route/lib/pages/home.dart b/examples/auto_route/lib/pages/home.dart new file mode 100644 index 0000000..e99d4aa --- /dev/null +++ b/examples/auto_route/lib/pages/home.dart @@ -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 createState() => _HomePageState(); +} + +class _HomePageState extends State { + @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()), + ), + ), + ); + } +} diff --git a/examples/auto_route/lib/router.dart b/examples/auto_route/lib/router.dart new file mode 100644 index 0000000..60b0f8b --- /dev/null +++ b/examples/auto_route/lib/router.dart @@ -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 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), + ], + ), + ]; +} diff --git a/examples/auto_route/lib/router.gr.dart b/examples/auto_route/lib/router.gr.dart new file mode 100644 index 0000000..0211395 --- /dev/null +++ b/examples/auto_route/lib/router.gr.dart @@ -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 { + BookRoute({Key? key, required int bookId, List? 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( + 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 { + const BooksRoute({List? 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 { + const BooksWrapperRoute({List? 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 { + const HomeRoute({List? children}) + : super(HomeRoute.name, initialChildren: children); + + static const String name = 'HomeRoute'; + + static PageInfo page = PageInfo( + name, + builder: (data) { + return const HomePage(); + }, + ); +} diff --git a/examples/auto_route/pubspec.yaml b/examples/auto_route/pubspec.yaml index 218cc78..d691270 100644 --- a/examples/auto_route/pubspec.yaml +++ b/examples/auto_route/pubspec.yaml @@ -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: @@ -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: