Skip to content

Commit ee1756e

Browse files
committed
refactor(router): Use ShellRoute for settings
- BlocProvider wraps all settings routes - Provides SettingsBloc to children - Simplifies settings route structure
1 parent 25715ae commit ee1756e

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

lib/router/router.dart

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -526,22 +526,19 @@ GoRouter createRouter({
526526
name: Routes.accountName,
527527
builder: (context, state) => const AccountPage(),
528528
routes: [
529-
// Sub-route for settings
530-
GoRoute(
531-
path: Routes.settings, // Relative path 'settings'
532-
name: Routes.settingsName,
533-
builder: (context, state) {
534-
// Provide SettingsBloc here for SettingsPage and its children
535-
// Access AppBloc to get the current user ID
529+
// ShellRoute for settings to provide SettingsBloc to children
530+
ShellRoute(
531+
builder: (BuildContext context, GoRouterState state, Widget child) {
532+
// This builder provides SettingsBloc to all routes within this ShellRoute.
533+
// 'child' will be SettingsPage, AppearanceSettingsPage, etc.
536534
final appBloc = context.read<AppBloc>();
537535
final userId = appBloc.state.user?.id;
538536

539-
return BlocProvider(
537+
return BlocProvider<SettingsBloc>(
540538
create: (context) {
541539
final settingsBloc = SettingsBloc(
542540
userAppSettingsRepository:
543-
context
544-
.read<HtDataRepository<UserAppSettings>>(),
541+
context.read<HtDataRepository<UserAppSettings>>(),
545542
);
546543
// Only load settings if a userId is available
547544
if (userId != null) {
@@ -550,39 +547,38 @@ GoRouter createRouter({
550547
);
551548
} else {
552549
// Handle case where user is unexpectedly null.
553-
// This might involve logging or emitting an error state
554-
// directly in SettingsBloc if it's designed to handle it,
555-
// or simply not loading settings.
556-
// For now, we'll assume router redirects prevent this.
557550
print(
558-
'Warning: User ID is null when creating SettingsBloc. Settings will not be loaded.',
551+
'ShellRoute/SettingsBloc: User ID is null when creating SettingsBloc. Settings will not be loaded.',
559552
);
560553
}
561554
return settingsBloc;
562555
},
563-
child: const SettingsPage(), // Use the actual page
556+
child: child, // child is the actual page widget (SettingsPage, AppearanceSettingsPage, etc.)
564557
);
565558
},
566-
// --- Settings Sub-Routes ---
567559
routes: [
568560
GoRoute(
569-
path: Routes.settingsAppearance, // 'appearance'
570-
name: Routes.settingsAppearanceName,
571-
builder:
572-
(context, state) => const AppearanceSettingsPage(),
573-
// SettingsBloc is inherited from parent route
574-
),
575-
GoRoute(
576-
path: Routes.settingsFeed, // 'feed'
577-
name: Routes.settingsFeedName,
578-
builder: (context, state) => const FeedSettingsPage(),
579-
),
580-
GoRoute(
581-
path: Routes.settingsNotifications, // 'notifications'
582-
name: Routes.settingsNotificationsName,
583-
builder:
584-
(context, state) =>
585-
const NotificationSettingsPage(),
561+
path: Routes.settings, // Relative path 'settings' from /account
562+
name: Routes.settingsName,
563+
builder: (context, state) => const SettingsPage(),
564+
// --- Settings Sub-Routes ---
565+
routes: [
566+
GoRoute(
567+
path: Routes.settingsAppearance, // 'appearance' relative to /account/settings
568+
name: Routes.settingsAppearanceName,
569+
builder: (context, state) => const AppearanceSettingsPage(),
570+
),
571+
GoRoute(
572+
path: Routes.settingsFeed, // 'feed' relative to /account/settings
573+
name: Routes.settingsFeedName,
574+
builder: (context, state) => const FeedSettingsPage(),
575+
),
576+
GoRoute(
577+
path: Routes.settingsNotifications, // 'notifications' relative to /account/settings
578+
name: Routes.settingsNotificationsName,
579+
builder: (context, state) => const NotificationSettingsPage(),
580+
),
581+
],
586582
),
587583
],
588584
),

0 commit comments

Comments
 (0)