From 1dc0103c6ce6de205e67df5a37ba0f4fb37f2b17 Mon Sep 17 00:00:00 2001 From: Alexandru Mariuti Date: Mon, 27 Jan 2025 12:52:08 +0100 Subject: [PATCH 1/4] chore: auto route example --- examples/auto_route/analysis_options.yaml | 6 +- examples/auto_route/lib/blocs/books/bloc.dart | 25 +++++ .../auto_route/lib/blocs/books/model.dart | 11 +++ examples/auto_route/lib/main.dart | 12 +-- examples/auto_route/lib/pages/book.dart | 25 +++++ examples/auto_route/lib/pages/books.dart | 53 ++++++++++ examples/auto_route/lib/pages/home.dart | 26 +++++ examples/auto_route/lib/router.dart | 26 +++++ examples/auto_route/lib/router.gr.dart | 97 +++++++++++++++++++ examples/auto_route/pubspec.yaml | 6 +- 10 files changed, 279 insertions(+), 8 deletions(-) create mode 100644 examples/auto_route/lib/blocs/books/bloc.dart create mode 100644 examples/auto_route/lib/blocs/books/model.dart create mode 100644 examples/auto_route/lib/pages/book.dart create mode 100644 examples/auto_route/lib/pages/books.dart create mode 100644 examples/auto_route/lib/pages/home.dart create mode 100644 examples/auto_route/lib/router.dart create mode 100644 examples/auto_route/lib/router.gr.dart 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/blocs/books/bloc.dart b/examples/auto_route/lib/blocs/books/bloc.dart new file mode 100644 index 0000000..0fb2c86 --- /dev/null +++ b/examples/auto_route/lib/blocs/books/bloc.dart @@ -0,0 +1,25 @@ +import 'package:auto_route_example/blocs/books/model.dart'; +import 'package:disco/disco.dart'; + +final booksBlocProvider = Provider((context) => BooksBloc()); + +class BooksBloc { + 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/blocs/books/model.dart b/examples/auto_route/lib/blocs/books/model.dart new file mode 100644 index 0000000..5ab8a28 --- /dev/null +++ b/examples/auto_route/lib/blocs/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..d563482 --- /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/blocs/books/bloc.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 booksBloc = booksBlocProvider.of(context); + final book = booksBloc.book(bookId); + return Scaffold( + appBar: AppBar(title: Text(book.name)), + body: Center( + child: Text(booksBloc.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..6589341 --- /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/blocs/books/bloc.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: [booksBlocProvider], + 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 booksBloc = booksBlocProvider.of(context); + final books = booksBloc.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..feaa23a 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,10 +10,14 @@ resolution: workspace dependencies: auto_route: ^9.3.0+1 + disco: + path: ../../packages/disco flutter: sdk: flutter dev_dependencies: + auto_route_generator: ^9.3.1 + build_runner: ^2.4.14 flutter_test: sdk: flutter very_good_analysis: ^7.0.0 From 63af3d04b525429845458035dffe3b174be36924 Mon Sep 17 00:00:00 2001 From: Alexandru Mariuti Date: Mon, 27 Jan 2025 15:17:22 +0100 Subject: [PATCH 2/4] refactor: rename bloc to controller --- .../books/bloc.dart => controllers/books/controller.dart} | 4 ++-- .../auto_route/lib/{blocs => controllers}/books/model.dart | 0 examples/auto_route/lib/pages/book.dart | 4 ++-- examples/auto_route/lib/pages/books.dart | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) rename examples/auto_route/lib/{blocs/books/bloc.dart => controllers/books/controller.dart} (75%) rename examples/auto_route/lib/{blocs => controllers}/books/model.dart (100%) diff --git a/examples/auto_route/lib/blocs/books/bloc.dart b/examples/auto_route/lib/controllers/books/controller.dart similarity index 75% rename from examples/auto_route/lib/blocs/books/bloc.dart rename to examples/auto_route/lib/controllers/books/controller.dart index 0fb2c86..400c342 100644 --- a/examples/auto_route/lib/blocs/books/bloc.dart +++ b/examples/auto_route/lib/controllers/books/controller.dart @@ -1,7 +1,7 @@ -import 'package:auto_route_example/blocs/books/model.dart'; +import 'package:auto_route_example/controllers/books/model.dart'; import 'package:disco/disco.dart'; -final booksBlocProvider = Provider((context) => BooksBloc()); +final booksControllerProvider = Provider((context) => BooksBloc()); class BooksBloc { final _books = List.generate( diff --git a/examples/auto_route/lib/blocs/books/model.dart b/examples/auto_route/lib/controllers/books/model.dart similarity index 100% rename from examples/auto_route/lib/blocs/books/model.dart rename to examples/auto_route/lib/controllers/books/model.dart diff --git a/examples/auto_route/lib/pages/book.dart b/examples/auto_route/lib/pages/book.dart index d563482..32e9002 100644 --- a/examples/auto_route/lib/pages/book.dart +++ b/examples/auto_route/lib/pages/book.dart @@ -1,5 +1,5 @@ import 'package:auto_route/annotations.dart'; -import 'package:auto_route_example/blocs/books/bloc.dart'; +import 'package:auto_route_example/controllers/books/controller.dart'; import 'package:flutter/material.dart'; @RoutePage() @@ -13,7 +13,7 @@ class BookPage extends StatelessWidget { @override Widget build(BuildContext context) { - final booksBloc = booksBlocProvider.of(context); + final booksBloc = booksControllerProvider.of(context); final book = booksBloc.book(bookId); return Scaffold( appBar: AppBar(title: Text(book.name)), diff --git a/examples/auto_route/lib/pages/books.dart b/examples/auto_route/lib/pages/books.dart index 6589341..a0a4818 100644 --- a/examples/auto_route/lib/pages/books.dart +++ b/examples/auto_route/lib/pages/books.dart @@ -1,5 +1,5 @@ import 'package:auto_route/auto_route.dart'; -import 'package:auto_route_example/blocs/books/bloc.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'; @@ -10,7 +10,7 @@ class BooksWrapperPage extends StatelessWidget implements AutoRouteWrapper { @override Widget wrappedRoute(BuildContext context) { return ProviderScope( - providers: [booksBlocProvider], + providers: [booksControllerProvider], child: this, ); } @@ -27,7 +27,7 @@ class BooksPage extends StatelessWidget { @override Widget build(BuildContext context) { - final booksBloc = booksBlocProvider.of(context); + final booksBloc = booksControllerProvider.of(context); final books = booksBloc.books; return Scaffold( From 8a2956c5831c3e545a6b20d7800f19f0c97bcab4 Mon Sep 17 00:00:00 2001 From: Alexandru Mariuti Date: Mon, 27 Jan 2025 15:20:38 +0100 Subject: [PATCH 3/4] chore: remove flutter_test dep --- examples/auto_route/pubspec.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/auto_route/pubspec.yaml b/examples/auto_route/pubspec.yaml index feaa23a..d691270 100644 --- a/examples/auto_route/pubspec.yaml +++ b/examples/auto_route/pubspec.yaml @@ -18,8 +18,6 @@ dependencies: dev_dependencies: auto_route_generator: ^9.3.1 build_runner: ^2.4.14 - flutter_test: - sdk: flutter very_good_analysis: ^7.0.0 flutter: From e5aa47d483b15dbaa264f3ee1bf6e1d63cc887e6 Mon Sep 17 00:00:00 2001 From: Manuel Plavsic <55398763+manuel-plavsic@users.noreply.github.com> Date: Mon, 27 Jan 2025 15:39:15 +0100 Subject: [PATCH 4/4] chore: rename bloc to controller in AutoRoute example (#10) Co-authored-by: Alexandru Mariuti --- examples/auto_route/lib/pages/book.dart | 6 +++--- examples/auto_route/lib/pages/books.dart | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/auto_route/lib/pages/book.dart b/examples/auto_route/lib/pages/book.dart index 32e9002..af17091 100644 --- a/examples/auto_route/lib/pages/book.dart +++ b/examples/auto_route/lib/pages/book.dart @@ -13,12 +13,12 @@ class BookPage extends StatelessWidget { @override Widget build(BuildContext context) { - final booksBloc = booksControllerProvider.of(context); - final book = booksBloc.book(bookId); + final booksController = booksControllerProvider.of(context); + final book = booksController.book(bookId); return Scaffold( appBar: AppBar(title: Text(book.name)), body: Center( - child: Text(booksBloc.book(bookId).description), + child: Text(booksController.book(bookId).description), ), ); } diff --git a/examples/auto_route/lib/pages/books.dart b/examples/auto_route/lib/pages/books.dart index a0a4818..fae696d 100644 --- a/examples/auto_route/lib/pages/books.dart +++ b/examples/auto_route/lib/pages/books.dart @@ -27,8 +27,8 @@ class BooksPage extends StatelessWidget { @override Widget build(BuildContext context) { - final booksBloc = booksControllerProvider.of(context); - final books = booksBloc.books; + final booksController = booksControllerProvider.of(context); + final books = booksController.books; return Scaffold( appBar: AppBar(