glp-companion/lib/app/scaffold/adaptive_home_scaffold.dart

113 lines
3.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
class AdaptiveHomeScaffold extends StatelessWidget {
final Widget child;
const AdaptiveHomeScaffold({
super.key,
required this.child,
});
int _calculateSelectedIndex(BuildContext context) {
final location = GoRouterState.of(context).uri.toString();
if (location.startsWith('/medication')) return 0;
if (location.startsWith('/nutrition')) return 1;
if (location.startsWith('/meal-planner')) return 2;
if (location.startsWith('/pantry')) return 3;
return 0;
}
void _onItemTapped(int index, BuildContext context) {
switch (index) {
case 0:
context.go('/medication');
break;
case 1:
context.go('/nutrition');
break;
case 2:
context.go('/meal-planner');
break;
case 3:
context.go('/pantry');
break;
}
}
@override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
final isDesktop = width >= 1024;
final selectedIndex = _calculateSelectedIndex(context);
if (isDesktop) {
return Scaffold(
body: Row(
children: [
NavigationRail(
selectedIndex: selectedIndex,
onDestinationSelected: (idx) => _onItemTapped(idx, context),
labelType: NavigationRailLabelType.all,
destinations: const [
NavigationRailDestination(
icon: Icon(Icons.medication_outlined),
selectedIcon: Icon(Icons.medication),
label: Text('Medication'),
),
NavigationRailDestination(
icon: Icon(Icons.restaurant_outlined),
selectedIcon: Icon(Icons.restaurant),
label: Text('Nutrition'),
),
NavigationRailDestination(
icon: Icon(Icons.calendar_month_outlined),
selectedIcon: Icon(Icons.calendar_month),
label: Text('Meal Plan'),
),
NavigationRailDestination(
icon: Icon(Icons.kitchen_outlined),
selectedIcon: Icon(Icons.kitchen),
label: Text('Pantry'),
),
],
),
const VerticalDivider(thickness: 1, width: 1),
Expanded(child: child),
],
),
);
}
return Scaffold(
body: child,
bottomNavigationBar: NavigationBar(
selectedIndex: selectedIndex,
onDestinationSelected: (idx) => _onItemTapped(idx, context),
destinations: const [
NavigationDestination(
icon: Icon(Icons.medication_outlined),
selectedIcon: Icon(Icons.medication),
label: 'Meds',
),
NavigationDestination(
icon: Icon(Icons.restaurant_outlined),
selectedIcon: Icon(Icons.restaurant),
label: 'Food',
),
NavigationDestination(
icon: Icon(Icons.calendar_month_outlined),
selectedIcon: Icon(Icons.calendar_month),
label: 'Plan',
),
NavigationDestination(
icon: Icon(Icons.kitchen_outlined),
selectedIcon: Icon(Icons.kitchen),
label: 'Pantry',
),
],
),
);
}
}