@@ -4,6 +4,7 @@ import 'package:flutter_adaptive_scaffold/flutter_adaptive_scaffold.dart';
44import 'package:flutter_bloc/flutter_bloc.dart' ;
55import 'package:flutter_news_app_web_dashboard_full_source_code/app/bloc/app_bloc.dart' ;
66import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/l10n.dart' ;
7+ import 'package:flutter_news_app_web_dashboard_full_source_code/router/route_permissions.dart' ;
78import 'package:flutter_news_app_web_dashboard_full_source_code/router/routes.dart' ;
89import 'package:go_router/go_router.dart' ;
910import 'package:ui_kit/ui_kit.dart' ;
@@ -58,30 +59,53 @@ class AppShell extends StatelessWidget {
5859 ),
5960 ];
6061
61- // Filter the destinations based on the user's role.
62- final accessibleDestinations = allDestinations.where ((destination) {
63- if (userRole == null ) return false ;
62+ // A parallel list of route names for permission checking, matching the
63+ // order of `allDestinations`.
64+ const allRouteNames = [
65+ Routes .overviewName,
66+ Routes .contentManagementName,
67+ Routes .userManagementName,
68+ Routes .appConfigurationName,
69+ ];
70+
71+ // Create a list of records containing the destination, its original
72+ // index, and its route name.
73+ final indexedDestinations = [
74+ for (var i = 0 ; i < allDestinations.length; i++ )
75+ (
76+ destination: allDestinations[i],
77+ originalIndex: i,
78+ routeName: allRouteNames[i],
79+ ),
80+ ];
6481
65- switch (userRole) {
66- case DashboardUserRole .admin:
67- // Admin can see all destinations.
68- return true ;
69- case DashboardUserRole .publisher:
70- // Publisher can only see Overview and Content Management.
71- return destination.label == l10n.overview ||
72- destination.label == l10n.contentManagement;
73- case DashboardUserRole .none:
74- return false ;
75- }
76- }).toList ();
82+ // Filter the destinations based on the user's role and allowed routes.
83+ final allowedRoutes = routePermissions[userRole] ?? {};
84+ final accessibleNavItems = indexedDestinations
85+ .where ((item) => allowedRoutes.contains (item.routeName))
86+ .toList ();
87+
88+ final accessibleDestinations = accessibleNavItems
89+ .map ((item) => item.destination)
90+ .toList ();
91+
92+ // Find the current index in the list of *accessible* destinations.
93+ final selectedIndex = accessibleNavItems.indexWhere (
94+ (item) => item.originalIndex == navigationShell.currentIndex,
95+ );
7796
7897 return Scaffold (
7998 body: AdaptiveScaffold (
80- selectedIndex: navigationShell.currentIndex ,
99+ selectedIndex: selectedIndex > - 1 ? selectedIndex : 0 ,
81100 onSelectedIndexChange: (index) {
101+ // Map the index from the accessible list back to the original
102+ // branch index.
103+ final originalBranchIndex =
104+ accessibleNavItems[index].originalIndex;
82105 navigationShell.goBranch (
83- index,
84- initialLocation: index == navigationShell.currentIndex,
106+ originalBranchIndex,
107+ initialLocation:
108+ originalBranchIndex == navigationShell.currentIndex,
85109 );
86110 },
87111 destinations: accessibleDestinations,
0 commit comments