import 'package:flutter/material.dart'; import '../api_service.dart'; import '../settings.dart'; import '../theme.dart'; import 'clock_screen.dart'; import 'leave_screen.dart'; import 'payslips_screen.dart'; import 'team_screen.dart'; import 'login_screen.dart'; import 'settings_screen.dart'; class HomeScreen extends StatefulWidget { final ApiService api; final Settings settings; const HomeScreen({super.key, required this.api, required this.settings}); @override State createState() => _HomeScreenState(); } class _HomeScreenState extends State { int _index = 0; void _logout() async { await widget.settings.clearSession(); if (!mounted) return; Navigator.pushReplacement(context, MaterialPageRoute(builder: (_) => LoginScreen(api: widget.api, settings: widget.settings))); } @override Widget build(BuildContext context) { final manager = widget.settings.isManager; final tabs = <_Tab>[ _Tab('Clock', Icons.access_time_outlined, Icons.access_time_filled, ClockScreen(api: widget.api, onAuthError: _logout)), _Tab('Leave', Icons.event_note_outlined, Icons.event_note, LeaveScreen(api: widget.api, onAuthError: _logout)), _Tab('Payslips', Icons.receipt_long_outlined, Icons.receipt_long, PayslipsScreen(api: widget.api, onAuthError: _logout)), if (manager) _Tab('Team', Icons.groups_outlined, Icons.groups, TeamScreen(api: widget.api, onAuthError: _logout)), ]; final i = _index.clamp(0, tabs.length - 1); return Scaffold( appBar: AppBar( titleSpacing: 20, title: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(tabs[i].label, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), if ((widget.settings.name ?? '').isNotEmpty) Text(widget.settings.name!, style: const TextStyle(fontSize: 12, color: AppColors.muted, fontWeight: FontWeight.normal)), ]), actions: [ IconButton( icon: const Icon(Icons.settings_outlined), onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (_) => SettingsScreen(settings: widget.settings, onLogout: _logout))), ), ], ), body: IndexedStack(index: i, children: tabs.map((t) => t.screen).toList()), bottomNavigationBar: NavigationBar( selectedIndex: i, onDestinationSelected: (v) => setState(() => _index = v), destinations: tabs .map((t) => NavigationDestination(icon: Icon(t.icon), selectedIcon: Icon(t.selected), label: t.label)) .toList(), ), ); } } class _Tab { final String label; final IconData icon, selected; final Widget screen; _Tab(this.label, this.icon, this.selected, this.screen); }