131 lines
3.4 KiB
Dart
131 lines
3.4 KiB
Dart
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<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
int _index = 0;
|
|
|
|
List<Widget> _screensForRole(String? role) {
|
|
final screens = <Widget>[const KasirScreen()];
|
|
if (role == 'admin') {
|
|
screens.addAll([
|
|
const ProdukScreen(),
|
|
const RiwayatScreen(),
|
|
const PengaturanScreen(),
|
|
]);
|
|
} else {
|
|
screens.addAll([
|
|
const RiwayatScreen(),
|
|
const PengaturanScreen(),
|
|
]);
|
|
}
|
|
return screens;
|
|
}
|
|
|
|
List<BottomNavigationBarItem> _navItemsForRole(String? role, AppState state) {
|
|
final items = <BottomNavigationBarItem>[
|
|
const BottomNavigationBarItem(
|
|
icon: Icon(Icons.point_of_sale),
|
|
label: 'Kasir',
|
|
),
|
|
];
|
|
if (role == 'admin') {
|
|
items.addAll([
|
|
const BottomNavigationBarItem(
|
|
icon: Icon(Icons.inventory_2),
|
|
label: 'Produk',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Badge(
|
|
isLabelVisible: state.unseenSalesCount > 0,
|
|
label: Text('${state.unseenSalesCount}'),
|
|
child: const 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<AppState>();
|
|
final screens = _screensForRole(state.userRole);
|
|
final navItems = _navItemsForRole(state.userRole, state);
|
|
|
|
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<AppState>().logout();
|
|
},
|
|
tooltip: 'Logout',
|
|
),
|
|
],
|
|
),
|
|
body: IndexedStack(
|
|
index: safeIndex,
|
|
children: screens,
|
|
),
|
|
bottomNavigationBar: navItems.length >= 2
|
|
? BottomNavigationBar(
|
|
currentIndex: safeIndex,
|
|
onTap: (i) {
|
|
setState(() => _index = i);
|
|
// Reset badge saat tab Riwayat dibuka.
|
|
final riwayatIdx = state.userRole == 'admin' ? 2 : 1;
|
|
if (i == riwayatIdx) {
|
|
context.read<AppState>().markSalesSeen();
|
|
}
|
|
},
|
|
items: navItems,
|
|
)
|
|
: null,
|
|
);
|
|
}
|
|
}
|
|
|