Refactor code structure for improved readability and maintainability
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -4,6 +4,13 @@ import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../features/auth/data/auth_providers.dart';
|
||||
|
||||
const _adminDestination = _AppDestination(
|
||||
path: '/admin',
|
||||
title: 'Admin',
|
||||
icon: Icons.admin_panel_settings_outlined,
|
||||
label: 'Admin',
|
||||
);
|
||||
|
||||
class AppShell extends ConsumerWidget {
|
||||
final String location;
|
||||
final Widget child;
|
||||
@@ -14,7 +21,7 @@ class AppShell extends ConsumerWidget {
|
||||
required this.child,
|
||||
});
|
||||
|
||||
static const _destinations = [
|
||||
static const _baseDestinations = [
|
||||
_AppDestination(
|
||||
path: '/recipes',
|
||||
title: 'Recept',
|
||||
@@ -53,8 +60,13 @@ class AppShell extends ConsumerWidget {
|
||||
),
|
||||
];
|
||||
|
||||
int _selectedIndex() {
|
||||
final index = _destinations.indexWhere(
|
||||
List<_AppDestination> _destinations(bool isAdmin) => [
|
||||
..._baseDestinations,
|
||||
if (isAdmin) _adminDestination,
|
||||
];
|
||||
|
||||
int _selectedIndex(List<_AppDestination> destinations) {
|
||||
final index = destinations.indexWhere(
|
||||
(destination) => location.startsWith(destination.path),
|
||||
);
|
||||
return index < 0 ? 0 : index;
|
||||
@@ -62,8 +74,10 @@ class AppShell extends ConsumerWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final selectedIndex = _selectedIndex();
|
||||
final selectedDestination = _destinations[selectedIndex];
|
||||
final isAdmin = ref.watch(isAdminProvider);
|
||||
final dests = _destinations(isAdmin);
|
||||
final selectedIndex = _selectedIndex(dests);
|
||||
final selectedDestination = dests[selectedIndex];
|
||||
final isWide = MediaQuery.of(context).size.width >= 900;
|
||||
|
||||
Future<void> logout() async {
|
||||
@@ -74,7 +88,7 @@ class AppShell extends ConsumerWidget {
|
||||
}
|
||||
|
||||
void navigateTo(int index) {
|
||||
final target = _destinations[index].path;
|
||||
final target = dests[index].path;
|
||||
if (target != location && context.mounted) {
|
||||
context.go(target);
|
||||
}
|
||||
@@ -98,7 +112,7 @@ class AppShell extends ConsumerWidget {
|
||||
selectedIndex: selectedIndex,
|
||||
onDestinationSelected: navigateTo,
|
||||
labelType: NavigationRailLabelType.all,
|
||||
destinations: _destinations
|
||||
destinations: dests
|
||||
.map(
|
||||
(destination) => NavigationRailDestination(
|
||||
icon: Icon(destination.icon),
|
||||
@@ -117,7 +131,7 @@ class AppShell extends ConsumerWidget {
|
||||
: NavigationBar(
|
||||
selectedIndex: selectedIndex,
|
||||
onDestinationSelected: navigateTo,
|
||||
destinations: _destinations
|
||||
destinations: dests
|
||||
.map(
|
||||
(destination) => NavigationDestination(
|
||||
icon: Icon(destination.icon),
|
||||
|
||||
Reference in New Issue
Block a user