import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'kasir_screen.dart'; import 'produk_screen.dart'; import 'riwayat_screen.dart'; import 'pengaturan_screen.dart'; import '../state/app_state.dart'; import '../utils/colors.dart'; class HomePage extends StatefulWidget { const HomePage({super.key}); @override State createState() => _HomePageState(); } class _HomePageState extends State { int _index = 0; List _screensForRole(String? role) { final screens = [const KasirScreen()]; if (role == 'admin') { screens.addAll([ const ProdukScreen(), const RiwayatScreen(), const PengaturanScreen(), ]); } else { screens.addAll([ const RiwayatScreen(), const PengaturanScreen(), ]); } return screens; } List _navItemsForRole(String? role) { final items = [ const BottomNavigationBarItem( icon: Icon(Icons.point_of_sale), label: 'Kasir', ), ]; if (role == 'admin') { items.addAll([ const BottomNavigationBarItem( icon: Icon(Icons.inventory_2), label: 'Produk', ), const BottomNavigationBarItem( icon: Icon(Icons.receipt_long), label: 'Riwayat', ), const BottomNavigationBarItem( icon: Icon(Icons.settings_outlined), label: 'Pengaturan', ), ]); } else { items.addAll([ const BottomNavigationBarItem( icon: Icon(Icons.receipt_long), label: 'Riwayat', ), const BottomNavigationBarItem( icon: Icon(Icons.settings_outlined), label: 'Pengaturan', ), ]); } return items; } @override Widget build(BuildContext context) { final state = context.watch(); final screens = _screensForRole(state.userRole); final navItems = _navItemsForRole(state.userRole); final safeIndex = _index >= screens.length ? 0 : _index; return Scaffold( appBar: AppBar( title: Row( mainAxisSize: MainAxisSize.min, children: [ const Icon(Icons.storefront_rounded, color: kAccent), const SizedBox(width: 8), const Text('Dutra Store'), ], ), centerTitle: true, elevation: 0, actions: [ IconButton( icon: const Icon(Icons.logout, color: kMuted), onPressed: () async { await context.read().logout(); }, tooltip: 'Logout', ), ], ), body: IndexedStack( index: safeIndex, children: screens, ), bottomNavigationBar: navItems.length >= 2 ? BottomNavigationBar( currentIndex: safeIndex, onTap: (i) => setState(() => _index = i), items: navItems, ) : null, ); } }