Files
oott/frontend/lib/navigation.dart
T

154 lines
4.6 KiB
Dart
Raw Normal View History

2026-02-11 14:07:27 -05:00
import 'package:flutter/material.dart';
import 'package:frontend/settings/settings.dart';
2026-02-11 14:07:27 -05:00
import 'package:go_router/go_router.dart';
import 'devices/device_detail.dart';
2026-05-27 14:58:11 -04:00
import 'devices/device_list.dart';
import 'notifications/notification_list.dart';
import 'utils/pref_utils.dart';
2026-02-11 14:07:27 -05:00
// Routes definitions
final GoRouter router = GoRouter(
// If there is no API base URL send the user to settings
initialLocation: '/notifications',
2026-02-11 14:07:27 -05:00
routes: [
ShellRoute(
builder: (context, state, child) {
return MainShell(child: child);
},
routes: [
GoRoute(
path: '/notifications',
name: 'notifications',
builder: (context, state) => NotificationList(),
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: '/settings',
name: 'settings',
builder: (context, state) => Settings(),
2026-02-11 14:07:27 -05:00
),
GoRoute(
path: '/about',
name: 'about',
builder: (context, state) => const Placeholder(),
),
],
),
],
);
// 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) {
return Scaffold(
appBar: AppBar(
title: const Text('OOTT'),
2026-02-11 18:31:12 -05:00
backgroundColor: Theme.of(
context,
).colorScheme.surfaceContainerLowest,
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,
2026-02-11 14:07:27 -05:00
extended: constraints.maxWidth >= 600,
destinations: [
NavigationRailDestination(
icon: Icon(Icons.notifications_outlined),
selectedIcon: Icon(Icons.notifications),
label: Text('Notifications'),
2026-02-11 14:07:27 -05:00
),
NavigationRailDestination(
icon: Icon(Icons.devices_other_outlined),
selectedIcon: Icon(Icons.devices_other),
label: Text('Devices'),
),
NavigationRailDestination(
icon: Icon(Icons.settings_outlined),
selectedIcon: Icon(Icons.settings),
label: Text('Settings'),
),
NavigationRailDestination(
icon: Icon(Icons.info_outline),
selectedIcon: Icon(Icons.info),
label: Text('About'),
),
],
selectedIndex: _calculateSelectedIndex(context),
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,
),
),
],
),
);
},
);
}
}
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('/settings')) return 2;
if (location.startsWith('/about')) return 3;
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('/settings');
break;
case 3:
context.go('/about');
break;
}
}