From deb060e0ae2efb5612fd181dc391da23b5ff7c3a Mon Sep 17 00:00:00 2001 From: Mahmoud Abduljawad Date: Mon, 25 Apr 2022 10:48:14 +0400 Subject: [PATCH] Refactor to allow re-using posts, post pages across app --- ios/Runner.xcodeproj/project.pbxproj | 4 +- .../xcshareddata/xcschemes/Runner.xcscheme | 2 +- lib/posts/posts_page.dart | 7 +- lib/posts/single_post_page.dart | 5 + lib/routes/router.dart | 22 ++- lib/routes/router.gr.dart | 152 ++++++++++++------ lib/users/user_profile_page.dart | 3 +- lib/users/users_page.dart | 7 +- 8 files changed, 134 insertions(+), 68 deletions(-) diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index d48b58a..c1904bf 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 46; + objectVersion = 50; objects = { /* Begin PBXBuildFile section */ @@ -127,7 +127,7 @@ 97C146E61CF9000F007C117D /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 1020; + LastUpgradeCheck = 1300; ORGANIZATIONNAME = ""; TargetAttributes = { 97C146ED1CF9000F007C117D = { diff --git a/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index a28140c..3db53b6 100644 --- a/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -1,6 +1,6 @@ context.router.push( - SinglePostRoute( - postId: posts[i].id, - ), - ), + onTileTap: () => + context.router.pushNamed('/post/${posts[i].id}'), ), ], ), diff --git a/lib/posts/single_post_page.dart b/lib/posts/single_post_page.dart index 165964d..661d821 100644 --- a/lib/posts/single_post_page.dart +++ b/lib/posts/single_post_page.dart @@ -1,4 +1,5 @@ import 'package:auto_route/annotations.dart'; +import 'package:auto_route/auto_route.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bottom_navigation_with_nested_routing_tutorial/data/app_data.dart'; @@ -13,6 +14,10 @@ class SinglePostPage extends StatelessWidget { Widget build(BuildContext context) { final post = Post.posts[postId - 1]; return Scaffold( + appBar: AppBar( + title: Text('Post #${post.id}'), + leading: const AutoBackButton(), + ), backgroundColor: post.color, body: Center( child: Padding( diff --git a/lib/routes/router.dart b/lib/routes/router.dart index 95ab160..7164650 100644 --- a/lib/routes/router.dart +++ b/lib/routes/router.dart @@ -19,10 +19,6 @@ import 'package:flutter_bottom_navigation_with_nested_routing_tutorial/users/use path: '', page: PostsPage, ), - AutoRoute( - path: ':postId', - page: SinglePostPage, - ) ], ), AutoRoute( @@ -35,17 +31,27 @@ import 'package:flutter_bottom_navigation_with_nested_routing_tutorial/users/use page: UsersPage, ), AutoRoute( - path: ':userId', - page: UserProfilePage, - ), + path: ':userId', + name: 'UserRouter', + page: EmptyRouterPage, + children: [ + AutoRoute(path: '', page: UserProfilePage), + AutoRoute( + path: 'posts', name: 'UserPostsRoute', page: PostsPage), + ]), ], ), AutoRoute( path: 'settings', name: 'SettingsRouter', page: SettingsPage, - ) + ), ]), + AutoRoute( + path: '/post', + name: 'PostRouter', + page: EmptyRouterPage, + children: [AutoRoute(path: ':postId', page: SinglePostPage)]) ], ) class $AppRouter {} diff --git a/lib/routes/router.gr.dart b/lib/routes/router.gr.dart index 4acbc88..7497aa8 100644 --- a/lib/routes/router.gr.dart +++ b/lib/routes/router.gr.dart @@ -15,10 +15,10 @@ import 'package:flutter/material.dart' as _i8; import '../home_page.dart' as _i1; import '../posts/posts_page.dart' as _i4; -import '../posts/single_post_page.dart' as _i5; +import '../posts/single_post_page.dart' as _i7; import '../settings/settings_page.dart' as _i3; -import '../users/user_profile_page.dart' as _i7; -import '../users/users_page.dart' as _i6; +import '../users/user_profile_page.dart' as _i6; +import '../users/users_page.dart' as _i5; class AppRouter extends _i2.RootStackRouter { AppRouter([_i8.GlobalKey<_i8.NavigatorState>? navigatorKey]) @@ -30,6 +30,10 @@ class AppRouter extends _i2.RootStackRouter { return _i2.MaterialPageX( routeData: routeData, child: const _i1.HomePage()); }, + PostRouter.name: (routeData) { + return _i2.MaterialPageX( + routeData: routeData, child: const _i2.EmptyRouterPage()); + }, PostsRouter.name: (routeData) { return _i2.MaterialPageX( routeData: routeData, child: const _i2.EmptyRouterPage()); @@ -48,18 +52,13 @@ class AppRouter extends _i2.RootStackRouter { return _i2.MaterialPageX( routeData: routeData, child: _i4.PostsPage(key: args.key)); }, - SinglePostRoute.name: (routeData) { - final pathParams = routeData.inheritedPathParams; - final args = routeData.argsAs( - orElse: () => - SinglePostRouteArgs(postId: pathParams.getInt('postId'))); + UsersRoute.name: (routeData) { return _i2.MaterialPageX( - routeData: routeData, - child: _i5.SinglePostPage(key: args.key, postId: args.postId)); + routeData: routeData, child: const _i5.UsersPage()); }, - UsersRoute.name: (routeData) { + UserRouter.name: (routeData) { return _i2.MaterialPageX( - routeData: routeData, child: const _i6.UsersPage()); + routeData: routeData, child: const _i2.EmptyRouterPage()); }, UserProfileRoute.name: (routeData) { final pathParams = routeData.inheritedPathParams; @@ -68,7 +67,22 @@ class AppRouter extends _i2.RootStackRouter { UserProfileRouteArgs(userId: pathParams.getInt('userId'))); return _i2.MaterialPageX( routeData: routeData, - child: _i7.UserProfilePage(key: args.key, userId: args.userId)); + child: _i6.UserProfilePage(key: args.key, userId: args.userId)); + }, + UserPostsRoute.name: (routeData) { + final args = routeData.argsAs( + orElse: () => const UserPostsRouteArgs()); + return _i2.MaterialPageX( + routeData: routeData, child: _i4.PostsPage(key: args.key)); + }, + SinglePostRoute.name: (routeData) { + final pathParams = routeData.inheritedPathParams; + final args = routeData.argsAs( + orElse: () => + SinglePostRouteArgs(postId: pathParams.getInt('postId'))); + return _i2.MaterialPageX( + routeData: routeData, + child: _i7.SinglePostPage(key: args.key, postId: args.postId)); } }; @@ -80,9 +94,7 @@ class AppRouter extends _i2.RootStackRouter { parent: HomeRoute.name, children: [ _i2.RouteConfig(PostsRoute.name, - path: '', parent: PostsRouter.name), - _i2.RouteConfig(SinglePostRoute.name, - path: ':postId', parent: PostsRouter.name) + path: '', parent: PostsRouter.name) ]), _i2.RouteConfig(UsersRouter.name, path: 'users', @@ -90,11 +102,22 @@ class AppRouter extends _i2.RootStackRouter { children: [ _i2.RouteConfig(UsersRoute.name, path: '', parent: UsersRouter.name), - _i2.RouteConfig(UserProfileRoute.name, - path: ':userId', parent: UsersRouter.name) + _i2.RouteConfig(UserRouter.name, + path: ':userId', + parent: UsersRouter.name, + children: [ + _i2.RouteConfig(UserProfileRoute.name, + path: '', parent: UserRouter.name), + _i2.RouteConfig(UserPostsRoute.name, + path: 'posts', parent: UserRouter.name) + ]) ]), _i2.RouteConfig(SettingsRouter.name, path: 'settings', parent: HomeRoute.name) + ]), + _i2.RouteConfig(PostRouter.name, path: '/post', children: [ + _i2.RouteConfig(SinglePostRoute.name, + path: ':postId', parent: PostRouter.name) ]) ]; } @@ -108,6 +131,15 @@ class HomeRoute extends _i2.PageRouteInfo { static const String name = 'HomeRoute'; } +/// generated route for +/// [_i2.EmptyRouterPage] +class PostRouter extends _i2.PageRouteInfo { + const PostRouter({List<_i2.PageRouteInfo>? children}) + : super(PostRouter.name, path: '/post', initialChildren: children); + + static const String name = 'PostRouter'; +} + /// generated route for /// [_i2.EmptyRouterPage] class PostsRouter extends _i2.PageRouteInfo { @@ -155,44 +187,28 @@ class PostsRouteArgs { } /// generated route for -/// [_i5.SinglePostPage] -class SinglePostRoute extends _i2.PageRouteInfo { - SinglePostRoute({_i8.Key? key, required int postId}) - : super(SinglePostRoute.name, - path: ':postId', - args: SinglePostRouteArgs(key: key, postId: postId), - rawPathParams: {'postId': postId}); - - static const String name = 'SinglePostRoute'; -} - -class SinglePostRouteArgs { - const SinglePostRouteArgs({this.key, required this.postId}); - - final _i8.Key? key; - - final int postId; +/// [_i5.UsersPage] +class UsersRoute extends _i2.PageRouteInfo { + const UsersRoute() : super(UsersRoute.name, path: ''); - @override - String toString() { - return 'SinglePostRouteArgs{key: $key, postId: $postId}'; - } + static const String name = 'UsersRoute'; } /// generated route for -/// [_i6.UsersPage] -class UsersRoute extends _i2.PageRouteInfo { - const UsersRoute() : super(UsersRoute.name, path: ''); +/// [_i2.EmptyRouterPage] +class UserRouter extends _i2.PageRouteInfo { + const UserRouter({List<_i2.PageRouteInfo>? children}) + : super(UserRouter.name, path: ':userId', initialChildren: children); - static const String name = 'UsersRoute'; + static const String name = 'UserRouter'; } /// generated route for -/// [_i7.UserProfilePage] +/// [_i6.UserProfilePage] class UserProfileRoute extends _i2.PageRouteInfo { UserProfileRoute({_i8.Key? key, required int userId}) : super(UserProfileRoute.name, - path: ':userId', + path: '', args: UserProfileRouteArgs(key: key, userId: userId), rawPathParams: {'userId': userId}); @@ -211,3 +227,49 @@ class UserProfileRouteArgs { return 'UserProfileRouteArgs{key: $key, userId: $userId}'; } } + +/// generated route for +/// [_i4.PostsPage] +class UserPostsRoute extends _i2.PageRouteInfo { + UserPostsRoute({_i8.Key? key}) + : super(UserPostsRoute.name, + path: 'posts', args: UserPostsRouteArgs(key: key)); + + static const String name = 'UserPostsRoute'; +} + +class UserPostsRouteArgs { + const UserPostsRouteArgs({this.key}); + + final _i8.Key? key; + + @override + String toString() { + return 'UserPostsRouteArgs{key: $key}'; + } +} + +/// generated route for +/// [_i7.SinglePostPage] +class SinglePostRoute extends _i2.PageRouteInfo { + SinglePostRoute({_i8.Key? key, required int postId}) + : super(SinglePostRoute.name, + path: ':postId', + args: SinglePostRouteArgs(key: key, postId: postId), + rawPathParams: {'postId': postId}); + + static const String name = 'SinglePostRoute'; +} + +class SinglePostRouteArgs { + const SinglePostRouteArgs({this.key, required this.postId}); + + final _i8.Key? key; + + final int postId; + + @override + String toString() { + return 'SinglePostRouteArgs{key: $key, postId: $postId}'; + } +} diff --git a/lib/users/user_profile_page.dart b/lib/users/user_profile_page.dart index 3ed2c55..d27f8f0 100644 --- a/lib/users/user_profile_page.dart +++ b/lib/users/user_profile_page.dart @@ -1,4 +1,4 @@ -import 'package:auto_route/annotations.dart'; +import 'package:auto_route/auto_route.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bottom_navigation_with_nested_routing_tutorial/data/app_data.dart'; import 'package:flutter_bottom_navigation_with_nested_routing_tutorial/widgets.dart'; @@ -22,6 +22,7 @@ class UserProfilePage extends StatelessWidget { UserAvatar( avatarColor: Colors.white, username: 'user${user.id}', + onAvatarTap: () => context.router.pushNamed('posts'), ) ], ), diff --git a/lib/users/users_page.dart b/lib/users/users_page.dart index abd0d64..71d656e 100644 --- a/lib/users/users_page.dart +++ b/lib/users/users_page.dart @@ -18,15 +18,10 @@ class UsersPage extends StatelessWidget { UserAvatar( avatarColor: users[i].color, username: 'user${users[i].id}', - onAvatarTap: () => context.router.push( - UserProfileRoute( - userId: users[i].id, - ), - ), + onAvatarTap: () => context.router.pushNamed('${users[i].id}'), ), ], ), ); } } -