-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add go router package and routing to the app #1401
base: main
Are you sure you want to change the base?
Changes from 4 commits
d1291db
f4a141d
6c51b3e
5e44123
5edd77a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -614,4 +614,4 @@ packages: | |
source: hosted | ||
version: "3.1.2" | ||
sdks: | ||
dart: ">=3.4.0 <4.0.0" | ||
dart: ">=3.5.0 <4.0.0" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,127 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_news_example/app/app.dart'; | ||
import 'package:flutter_news_example/article/article.dart'; | ||
import 'package:flutter_news_example/home/home.dart'; | ||
import 'package:flutter_news_example/login/login.dart'; | ||
import 'package:flutter_news_example/magic_link_prompt/view/magic_link_prompt_page.dart'; | ||
import 'package:flutter_news_example/network_error/network_error.dart'; | ||
import 'package:flutter_news_example/notification_preferences/notification_preferences.dart'; | ||
import 'package:flutter_news_example/onboarding/onboarding.dart'; | ||
import 'package:flutter_news_example/slideshow/slideshow.dart'; | ||
import 'package:flutter_news_example/subscriptions/view/manage_subscription_page.dart'; | ||
import 'package:flutter_news_example/user_profile/user_profile.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
import 'package:news_blocks/news_blocks.dart'; | ||
|
||
List<Page<dynamic>> onGenerateAppViewPages( | ||
AppStatus state, | ||
List<Page<dynamic>> pages, | ||
) { | ||
switch (state) { | ||
case AppStatus.onboardingRequired: | ||
return [OnboardingPage.page()]; | ||
case AppStatus.unauthenticated: | ||
case AppStatus.authenticated: | ||
return [HomePage.page()]; | ||
} | ||
} | ||
final GoRouter router = GoRouter( | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: HomePage.routePath, | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const HomePage(); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
name: NetworkErrorPage.routePath, | ||
path: NetworkErrorPage.routePath, | ||
builder: (BuildContext context, GoRouterState state) { | ||
final onRetry = state.extra as VoidCallback?; | ||
return NetworkError(onRetry: onRetry); | ||
}, | ||
), | ||
GoRoute( | ||
name: LoginWithEmailPage.routePath, | ||
path: LoginWithEmailPage.routePath, | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const LoginWithEmailPage(); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
name: MagicLinkPromptPage.routePath, | ||
path: MagicLinkPromptPage.routePath, | ||
builder: (BuildContext context, GoRouterState state) { | ||
return MagicLinkPromptPage( | ||
email: state.uri.queryParameters['email']!, | ||
); | ||
}, | ||
), | ||
], | ||
), | ||
GoRoute( | ||
name: ArticlePage.routeName, | ||
path: ArticlePage.routePath, | ||
builder: (BuildContext context, GoRouterState state) { | ||
final id = state.pathParameters['id']; | ||
|
||
final isVideoArticle = bool.tryParse( | ||
state.uri.queryParameters['isVideoArticle'] ?? 'false', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) ?? | ||
false; | ||
final interstitialAdBehavior = | ||
state.uri.queryParameters['interstitialAdBehavior'] != null | ||
? InterstitialAdBehavior.values.firstWhere( | ||
(e) => | ||
e.toString() == | ||
'InterstitialAdBehavior.' | ||
// ignore: lines_longer_than_80_chars | ||
'${state.uri.queryParameters['interstitialAdBehavior']}', | ||
) | ||
: null; | ||
|
||
if (id == null) { | ||
throw Exception('Missing required "id" parameter'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's shown when this happens ? Isn't better to render |
||
} | ||
|
||
return ArticlePage( | ||
id: id, | ||
isVideoArticle: isVideoArticle, | ||
interstitialAdBehavior: | ||
interstitialAdBehavior ?? InterstitialAdBehavior.onOpen, | ||
); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
name: SlideshowPage.routePath, | ||
path: SlideshowPage.routePath, | ||
builder: (BuildContext context, GoRouterState state) { | ||
return SlideshowPage( | ||
slideshow: state.extra! as SlideshowBlock, | ||
articleId: state.pathParameters['id']!, | ||
); | ||
}, | ||
), | ||
], | ||
), | ||
GoRoute( | ||
name: UserProfilePage.routePath, | ||
path: UserProfilePage.routePath, | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const UserProfilePage(); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
name: ManageSubscriptionPage.routePath, | ||
path: ManageSubscriptionPage.routePath, | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const ManageSubscriptionPage(); | ||
}, | ||
), | ||
GoRoute( | ||
name: NotificationPreferencesPage.routePath, | ||
path: NotificationPreferencesPage.routePath, | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const NotificationPreferencesPage(); | ||
}, | ||
), | ||
], | ||
), | ||
], | ||
), | ||
GoRoute( | ||
name: OnboardingPage.routePath, | ||
path: OnboardingPage.routePath, | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const OnboardingPage(); | ||
}, | ||
), | ||
], | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,10 @@ import 'package:flutter_news_example/feed/feed.dart'; | |
import 'package:flutter_news_example/home/home.dart'; | ||
import 'package:flutter_news_example/login/login.dart'; | ||
import 'package:flutter_news_example/navigation/navigation.dart'; | ||
import 'package:flutter_news_example/onboarding/view/onboarding_page.dart'; | ||
import 'package:flutter_news_example/search/search.dart'; | ||
import 'package:flutter_news_example/user_profile/user_profile.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
class HomeView extends StatelessWidget { | ||
const HomeView({super.key}); | ||
|
@@ -31,6 +33,17 @@ class HomeView extends StatelessWidget { | |
} | ||
}, | ||
), | ||
BlocListener<AppBloc, AppState>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Something like this could be simpler, what do you think? |
||
listener: (context, state) { | ||
switch (state.status) { | ||
case AppStatus.onboardingRequired: | ||
context.goNamed(OnboardingPage.routePath); | ||
case AppStatus.unauthenticated: | ||
case AppStatus.authenticated: | ||
return; | ||
} | ||
}, | ||
), | ||
BlocListener<HomeCubit, HomeState>( | ||
listener: (context, state) { | ||
FocusManager.instance.primaryFocus?.unfocus(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to be able to use barrel files instead of importing the file directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check other imports too