Skip to content

Commit

Permalink
Refactor to allow re-using posts, post pages across app
Browse files Browse the repository at this point in the history
  • Loading branch information
mahmoudajawad authored and ali4fun2u committed Apr 25, 2022
1 parent 8f7abab commit deb060e
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 68 deletions.
4 changes: 2 additions & 2 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 50;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -127,7 +127,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1020;
LastUpgradeCheck = 1300;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
LastUpgradeVersion = "1300"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
7 changes: 2 additions & 5 deletions lib/posts/posts_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ class PostsPage extends StatelessWidget {
PostTile(
tileColor: posts[i].color,
postTitle: posts[i].title,
onTileTap: () => context.router.push(
SinglePostRoute(
postId: posts[i].id,
),
),
onTileTap: () =>
context.router.pushNamed('/post/${posts[i].id}'),
),
],
),
Expand Down
5 changes: 5 additions & 0 deletions lib/posts/single_post_page.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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(
Expand Down
22 changes: 14 additions & 8 deletions lib/routes/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ import 'package:flutter_bottom_navigation_with_nested_routing_tutorial/users/use
path: '',
page: PostsPage,
),
AutoRoute(
path: ':postId',
page: SinglePostPage,
)
],
),
AutoRoute(
Expand All @@ -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 {}
152 changes: 107 additions & 45 deletions lib/routes/router.gr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -30,6 +30,10 @@ class AppRouter extends _i2.RootStackRouter {
return _i2.MaterialPageX<dynamic>(
routeData: routeData, child: const _i1.HomePage());
},
PostRouter.name: (routeData) {
return _i2.MaterialPageX<dynamic>(
routeData: routeData, child: const _i2.EmptyRouterPage());
},
PostsRouter.name: (routeData) {
return _i2.MaterialPageX<dynamic>(
routeData: routeData, child: const _i2.EmptyRouterPage());
Expand All @@ -48,18 +52,13 @@ class AppRouter extends _i2.RootStackRouter {
return _i2.MaterialPageX<dynamic>(
routeData: routeData, child: _i4.PostsPage(key: args.key));
},
SinglePostRoute.name: (routeData) {
final pathParams = routeData.inheritedPathParams;
final args = routeData.argsAs<SinglePostRouteArgs>(
orElse: () =>
SinglePostRouteArgs(postId: pathParams.getInt('postId')));
UsersRoute.name: (routeData) {
return _i2.MaterialPageX<dynamic>(
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<dynamic>(
routeData: routeData, child: const _i6.UsersPage());
routeData: routeData, child: const _i2.EmptyRouterPage());
},
UserProfileRoute.name: (routeData) {
final pathParams = routeData.inheritedPathParams;
Expand All @@ -68,7 +67,22 @@ class AppRouter extends _i2.RootStackRouter {
UserProfileRouteArgs(userId: pathParams.getInt('userId')));
return _i2.MaterialPageX<dynamic>(
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<UserPostsRouteArgs>(
orElse: () => const UserPostsRouteArgs());
return _i2.MaterialPageX<dynamic>(
routeData: routeData, child: _i4.PostsPage(key: args.key));
},
SinglePostRoute.name: (routeData) {
final pathParams = routeData.inheritedPathParams;
final args = routeData.argsAs<SinglePostRouteArgs>(
orElse: () =>
SinglePostRouteArgs(postId: pathParams.getInt('postId')));
return _i2.MaterialPageX<dynamic>(
routeData: routeData,
child: _i7.SinglePostPage(key: args.key, postId: args.postId));
}
};

Expand All @@ -80,21 +94,30 @@ 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',
parent: HomeRoute.name,
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)
])
];
}
Expand All @@ -108,6 +131,15 @@ class HomeRoute extends _i2.PageRouteInfo<void> {
static const String name = 'HomeRoute';
}

/// generated route for
/// [_i2.EmptyRouterPage]
class PostRouter extends _i2.PageRouteInfo<void> {
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<void> {
Expand Down Expand Up @@ -155,44 +187,28 @@ class PostsRouteArgs {
}

/// generated route for
/// [_i5.SinglePostPage]
class SinglePostRoute extends _i2.PageRouteInfo<SinglePostRouteArgs> {
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<void> {
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<void> {
const UsersRoute() : super(UsersRoute.name, path: '');
/// [_i2.EmptyRouterPage]
class UserRouter extends _i2.PageRouteInfo<void> {
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<UserProfileRouteArgs> {
UserProfileRoute({_i8.Key? key, required int userId})
: super(UserProfileRoute.name,
path: ':userId',
path: '',
args: UserProfileRouteArgs(key: key, userId: userId),
rawPathParams: {'userId': userId});

Expand All @@ -211,3 +227,49 @@ class UserProfileRouteArgs {
return 'UserProfileRouteArgs{key: $key, userId: $userId}';
}
}

/// generated route for
/// [_i4.PostsPage]
class UserPostsRoute extends _i2.PageRouteInfo<UserPostsRouteArgs> {
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<SinglePostRouteArgs> {
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}';
}
}
3 changes: 2 additions & 1 deletion lib/users/user_profile_page.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -22,6 +22,7 @@ class UserProfilePage extends StatelessWidget {
UserAvatar(
avatarColor: Colors.white,
username: 'user${user.id}',
onAvatarTap: () => context.router.pushNamed('posts'),
)
],
),
Expand Down
7 changes: 1 addition & 6 deletions lib/users/users_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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}'),
),
],
),
);
}
}

0 comments on commit deb060e

Please sign in to comment.