Reviewed-on: #11 Co-authored-by: Mark M2 Macbook <marco@catelli.it> Co-committed-by: Mark M2 Macbook <marco@catelli.it>
97 lines
3.3 KiB
Dart
97 lines
3.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
class AppShell extends StatelessWidget {
|
|
final Widget child;
|
|
|
|
const AppShell({super.key, required this.child});
|
|
|
|
// Calcoliamo l'indice attivo in base all'URL corrente!
|
|
int _calculateSelectedIndex(BuildContext context) {
|
|
final String location = GoRouterState.of(context).uri.path;
|
|
if (location.startsWith('/master-data')) return 1;
|
|
if (location.startsWith('/settings')) return 2;
|
|
return 0; // Default: Dashboard
|
|
}
|
|
|
|
void _onItemTapped(int index, BuildContext context) {
|
|
switch (index) {
|
|
case 0:
|
|
context.go('/');
|
|
break;
|
|
case 1:
|
|
context.go('/master-data');
|
|
break;
|
|
case 2:
|
|
context.go('/settings');
|
|
break;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final currentIndex = _calculateSelectedIndex(context);
|
|
// Breakpoint: se lo schermo è più largo di 600px, usiamo la Rail laterale
|
|
final isDesktop = MediaQuery.sizeOf(context).width >= 600;
|
|
|
|
return Scaffold(
|
|
body: isDesktop
|
|
? Row(
|
|
children: [
|
|
NavigationRail(
|
|
selectedIndex: currentIndex,
|
|
onDestinationSelected: (index) =>
|
|
_onItemTapped(index, context),
|
|
labelType: NavigationRailLabelType.all,
|
|
destinations: const [
|
|
NavigationRailDestination(
|
|
icon: Icon(Icons.dashboard_outlined),
|
|
selectedIcon: Icon(Icons.dashboard),
|
|
label: Text('Dashboard'),
|
|
),
|
|
NavigationRailDestination(
|
|
icon: Icon(Icons.folder_special_outlined),
|
|
selectedIcon: Icon(Icons.folder_special),
|
|
label: Text('Anagrafiche'),
|
|
),
|
|
NavigationRailDestination(
|
|
icon: Icon(Icons.settings_outlined),
|
|
selectedIcon: Icon(Icons.settings),
|
|
label: Text('Impostazioni'),
|
|
),
|
|
],
|
|
),
|
|
const VerticalDivider(thickness: 1, width: 1),
|
|
// Il contenuto della pagina
|
|
Expanded(child: child),
|
|
],
|
|
)
|
|
: child, // Su mobile il contenuto prende tutto lo schermo...
|
|
// ... e mettiamo la barra in basso!
|
|
bottomNavigationBar: isDesktop
|
|
? null
|
|
: NavigationBar(
|
|
selectedIndex: currentIndex,
|
|
onDestinationSelected: (index) => _onItemTapped(index, context),
|
|
destinations: const [
|
|
NavigationDestination(
|
|
icon: Icon(Icons.dashboard_outlined),
|
|
selectedIcon: Icon(Icons.dashboard),
|
|
label: 'Dashboard',
|
|
),
|
|
NavigationDestination(
|
|
icon: Icon(Icons.folder_special_outlined),
|
|
selectedIcon: Icon(Icons.folder_special),
|
|
label: 'Anagrafiche',
|
|
),
|
|
NavigationDestination(
|
|
icon: Icon(Icons.settings_outlined),
|
|
selectedIcon: Icon(Icons.settings),
|
|
label: 'Impostazioni',
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|