Files
oott/frontend/lib/navigation.dart
T

228 lines
6.6 KiB
Dart
Raw Normal View History

2026-02-11 14:07:27 -05:00
import 'package:flutter/material.dart';
import 'package:frontend/about/about.dart';
import 'package:frontend/settings/settings.dart';
2026-02-11 14:07:27 -05:00
import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart';
import 'devices/device_detail.dart';
2026-05-27 14:58:11 -04:00
import 'devices/device_list.dart';
import 'home/home_screen.dart';
import 'status/status_screen.dart';
import 'utils/pref_utils.dart';
2026-02-11 14:07:27 -05:00
// M3 window size class breakpoints
const _mediumBreakpoint = 600.0;
const _expandedBreakpoint = 840.0;
typedef _NavDest = ({IconData icon, IconData activeIcon, String label});
const List<_NavDest> _destinations = [
(icon: Icons.home_outlined, activeIcon: Icons.home, label: 'Home'),
(
icon: Icons.devices_other_outlined,
activeIcon: Icons.devices_other,
label: 'Devices',
),
(
icon: Icons.monitor_heart_outlined,
activeIcon: Icons.monitor_heart,
label: 'Status',
),
(
icon: Icons.settings_outlined,
activeIcon: Icons.settings,
label: 'Settings',
),
(icon: Icons.info_outline, activeIcon: Icons.info, label: 'About'),
];
// Observer used to notify subscribed routes when another route is pushed
// on top of or popped from them, so they can refresh stale data.
final RouteObserver<ModalRoute<void>> routeObserver =
RouteObserver<ModalRoute<void>>();
2026-02-11 14:07:27 -05:00
// Routes definitions
final GoRouter router = GoRouter(
initialLocation: '/notifications',
2026-02-11 14:07:27 -05:00
routes: [
ShellRoute(
observers: [routeObserver],
2026-02-11 14:07:27 -05:00
builder: (context, state, child) {
return MainShell(child: child);
},
routes: [
GoRoute(
path: '/notifications',
name: 'notifications',
builder: (context, state) => const HomeScreen(),
redirect: (context, state) => _redirectToSettings(),
2026-02-11 14:07:27 -05:00
),
GoRoute(
path: '/devices',
name: 'devices',
2026-05-27 14:58:11 -04:00
builder: (context, state) => const DeviceList(),
redirect: (context, state) => _redirectToSettings(),
routes: [
GoRoute(
path: ':macAddress',
name: 'deviceDetail',
builder: (context, state) {
final mac = state.pathParameters['macAddress']!;
return DeviceDetail(macAddress: mac);
},
),
],
2026-02-11 14:07:27 -05:00
),
GoRoute(
path: '/status',
name: 'status',
builder: (context, state) => const StatusScreen(),
redirect: (context, state) => _redirectToSettings(),
),
2026-02-11 14:07:27 -05:00
GoRoute(
path: '/settings',
name: 'settings',
builder: (context, state) => Settings(),
2026-02-11 14:07:27 -05:00
),
GoRoute(
path: '/about',
name: 'about',
builder: (context, state) => const About(),
2026-02-11 14:07:27 -05:00
),
],
),
],
);
// Application shell and navigation
class MainShell extends StatelessWidget {
final Widget child;
const MainShell({super.key, required this.child});
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
final selectedIndex = _calculateSelectedIndex(context);
final width = constraints.maxWidth;
if (width < _mediumBreakpoint) {
return Scaffold(
appBar: _buildAppBar(context),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: child,
),
bottomNavigationBar: NavigationBar(
selectedIndex: selectedIndex,
onDestinationSelected: (index) =>
_onDestinationSelected(index, context),
destinations: _destinations
.map(
(d) => NavigationDestination(
icon: Icon(d.icon),
selectedIcon: Icon(d.activeIcon),
label: d.label,
),
)
.toList(),
),
);
}
return Scaffold(
appBar: _buildAppBar(context),
2026-02-11 14:07:27 -05:00
body: Row(
children: [
SafeArea(
child: NavigationRail(
backgroundColor: Theme.of(
context,
2026-02-11 18:31:12 -05:00
).colorScheme.surfaceContainerLow,
extended: width >= _expandedBreakpoint,
destinations: _destinations
.map(
(d) => NavigationRailDestination(
icon: Icon(d.icon),
selectedIcon: Icon(d.activeIcon),
label: Text(d.label),
),
)
.toList(),
selectedIndex: selectedIndex,
2026-02-11 14:07:27 -05:00
onDestinationSelected: (index) =>
_onDestinationSelected(index, context),
),
),
Expanded(
child: Container(
padding: const EdgeInsets.all(20),
2026-02-11 18:31:12 -05:00
color: Theme.of(context).colorScheme.surface,
2026-02-11 14:07:27 -05:00
child: child,
),
),
],
),
);
},
);
}
AppBar _buildAppBar(BuildContext context) {
return AppBar(
title: Container(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 2),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary,
borderRadius: BorderRadius.circular(10),
),
child: Text(
'OOTT',
style: GoogleFonts.barlowCondensed(
color: Theme.of(context).colorScheme.onPrimary,
fontWeight: FontWeight.bold,
fontSize: 26,
),
),
),
backgroundColor: Theme.of(context).colorScheme.surfaceContainerLowest,
);
}
2026-02-11 14:07:27 -05:00
}
String? _redirectToSettings() {
return (PrefUtil.getValue("base_url", "") as String == "")
? '/settings'
: null;
}
2026-02-11 14:07:27 -05:00
int _calculateSelectedIndex(BuildContext context) {
final location = GoRouterState.of(context).uri.path;
if (location.startsWith('/notifications')) return 0;
2026-02-11 14:07:27 -05:00
if (location.startsWith('/devices')) return 1;
if (location.startsWith('/status')) return 2;
if (location.startsWith('/settings')) return 3;
if (location.startsWith('/about')) return 4;
2026-02-11 14:07:27 -05:00
return 0;
}
void _onDestinationSelected(int index, BuildContext context) {
switch (index) {
case 0:
context.go('/notifications');
2026-02-11 14:07:27 -05:00
break;
case 1:
context.go('/devices');
break;
case 2:
context.go('/status');
2026-02-11 14:07:27 -05:00
break;
case 3:
context.go('/settings');
break;
case 4:
2026-02-11 14:07:27 -05:00
context.go('/about');
break;
}
}