768 lines
26 KiB
Dart
Raw Normal View History

2025-09-20 03:10:05 +07:00
// ========================================
2025-09-20 04:36:22 +07:00
// HOMEPAGE - LOCAL DATA ONLY, NO SYNC
2025-09-20 03:10:05 +07:00
// lib/presentation/home/pages/home_page.dart
// ========================================
import 'dart:developer';
import 'package:enaklo_pos/core/components/flushbar.dart';
2025-09-20 04:36:22 +07:00
import 'package:enaklo_pos/data/models/response/category_response_model.dart';
2025-08-06 17:41:04 +07:00
import 'package:enaklo_pos/presentation/home/bloc/category_loader/category_loader_bloc.dart';
2025-08-04 13:15:03 +07:00
import 'package:enaklo_pos/presentation/home/bloc/current_outlet/current_outlet_bloc.dart';
2025-08-03 00:35:00 +07:00
import 'package:enaklo_pos/presentation/home/bloc/product_loader/product_loader_bloc.dart';
2025-08-05 15:25:10 +07:00
import 'package:enaklo_pos/presentation/home/bloc/user_update_outlet/user_update_outlet_bloc.dart';
2025-09-01 22:12:56 +07:00
import 'package:enaklo_pos/presentation/home/models/product_quantity.dart';
2025-08-06 17:41:04 +07:00
import 'package:enaklo_pos/presentation/home/widgets/category_tab_bar.dart';
2025-07-31 19:25:45 +07:00
import 'package:enaklo_pos/presentation/home/widgets/home_right_title.dart';
2025-07-30 22:38:44 +07:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:enaklo_pos/core/extensions/build_context_ext.dart';
import 'package:enaklo_pos/core/extensions/int_ext.dart';
import 'package:enaklo_pos/data/models/response/table_model.dart';
import 'package:enaklo_pos/presentation/home/pages/confirm_payment_page.dart';
import '../../../core/assets/assets.gen.dart';
import '../../../core/components/buttons.dart';
import '../../../core/components/spaces.dart';
import '../../../core/constants/colors.dart';
import '../bloc/checkout/checkout_bloc.dart';
import '../widgets/home_title.dart';
import '../widgets/order_menu.dart';
import '../widgets/product_card.dart';
class HomePage extends StatefulWidget {
final bool isTable;
final TableModel? table;
2025-09-01 22:12:56 +07:00
final List<ProductQuantity> items;
2025-07-30 22:38:44 +07:00
const HomePage({
2025-07-31 23:22:34 +07:00
super.key,
2025-07-30 22:38:44 +07:00
required this.isTable,
this.table,
2025-09-01 22:12:56 +07:00
required this.items,
2025-07-31 23:22:34 +07:00
});
2025-07-30 22:38:44 +07:00
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final searchController = TextEditingController();
2025-08-05 19:48:32 +07:00
final ScrollController scrollController = ScrollController();
2025-07-30 22:38:44 +07:00
String searchQuery = '';
@override
void initState() {
super.initState();
2025-09-20 04:36:22 +07:00
_loadData();
2025-07-30 22:38:44 +07:00
}
2025-08-06 17:41:04 +07:00
@override
void dispose() {
searchController.dispose();
scrollController.dispose();
super.dispose();
}
2025-09-20 04:36:22 +07:00
void _loadData() {
log('📱 Loading data from local database...');
2025-09-20 03:10:05 +07:00
2025-09-20 04:36:22 +07:00
// Load categories from local database
context
.read<CategoryLoaderBloc>()
.add(const CategoryLoaderEvent.getCategories());
2025-07-31 19:25:45 +07:00
2025-09-20 04:36:22 +07:00
// Load products from local database
2025-07-30 22:38:44 +07:00
context
2025-08-03 00:35:00 +07:00
.read<ProductLoaderBloc>()
.add(const ProductLoaderEvent.getProduct());
2025-07-31 19:25:45 +07:00
2025-09-20 03:10:05 +07:00
// Initialize other components
2025-09-01 22:12:56 +07:00
context.read<CheckoutBloc>().add(CheckoutEvent.started(widget.items));
2025-08-04 13:15:03 +07:00
context.read<CurrentOutletBloc>().add(CurrentOutletEvent.currentOutlet());
2025-07-30 22:38:44 +07:00
}
2025-09-20 04:36:22 +07:00
void _refreshData() {
2025-09-20 03:10:05 +07:00
log('🔄 Refreshing local data...');
context.read<ProductLoaderBloc>().add(const ProductLoaderEvent.refresh());
2025-09-20 04:36:22 +07:00
context.read<CategoryLoaderBloc>().add(const CategoryLoaderEvent.refresh());
2025-09-20 03:10:05 +07:00
}
2025-07-30 22:38:44 +07:00
void onCategoryTap(int index) {
searchController.clear();
setState(() {
searchQuery = '';
});
}
2025-08-07 12:19:25 +07:00
bool _handleScrollNotification(
ScrollNotification notification, String? categoryId) {
2025-08-06 17:41:04 +07:00
if (notification is ScrollEndNotification &&
scrollController.position.extentAfter == 0) {
2025-09-20 04:36:22 +07:00
log('📄 Loading more products...');
2025-08-07 12:19:25 +07:00
context.read<ProductLoaderBloc>().add(
2025-09-20 04:36:22 +07:00
ProductLoaderEvent.loadMore(),
2025-08-07 12:19:25 +07:00
);
2025-08-06 17:41:04 +07:00
return true;
}
return false;
2025-07-30 22:38:44 +07:00
}
@override
Widget build(BuildContext context) {
2025-08-05 15:25:10 +07:00
return BlocListener<UserUpdateOutletBloc, UserUpdateOutletState>(
listener: (context, state) {
state.maybeWhen(
orElse: () {},
success: () {
Future.delayed(Duration(milliseconds: 300), () {
AppFlushbar.showSuccess(context, 'Outlet berhasil diubah');
context
.read<CurrentOutletBloc>()
.add(CurrentOutletEvent.currentOutlet());
});
},
error: (message) => AppFlushbar.showError(context, message),
);
},
child: Hero(
tag: 'confirmation_screen',
child: Scaffold(
backgroundColor: AppColors.white,
2025-09-20 03:10:05 +07:00
body: Column(
2025-08-05 15:25:10 +07:00
children: [
2025-09-20 04:36:22 +07:00
// Simple local mode indicator
_buildLocalIndicator(),
2025-09-20 03:10:05 +07:00
// Main content
2025-08-05 15:25:10 +07:00
Expanded(
2025-09-20 03:10:05 +07:00
child: Row(
children: [
2025-09-20 04:36:22 +07:00
// Left panel - Products with Categories
2025-09-20 03:10:05 +07:00
Expanded(
flex: 3,
2025-09-20 04:36:22 +07:00
child:
BlocBuilder<CategoryLoaderBloc, CategoryLoaderState>(
builder: (context, categoryState) {
return categoryState.maybeWhen(
orElse: () => _buildCategoryLoadingState(),
loading: () => _buildCategoryLoadingState(),
error: (message) =>
_buildCategoryErrorState(message),
loaded: (categories, hasReachedMax, currentPage,
isLoadingMore, isActive, searchQuery) =>
_buildCategoryContent(categories),
);
},
2025-09-20 03:10:05 +07:00
),
),
2025-09-20 04:36:22 +07:00
// Right panel - Cart
2025-09-20 03:10:05 +07:00
Expanded(
flex: 2,
child: _buildCartSection(),
),
],
),
),
],
),
),
),
);
}
2025-09-20 04:36:22 +07:00
// Simple local mode indicator without sync
Widget _buildLocalIndicator() {
2025-09-20 03:10:05 +07:00
return Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
color: Colors.blue.shade600,
child: Row(
children: [
Icon(Icons.storage, color: Colors.white, size: 16),
SizedBox(width: 8),
Expanded(
child: Text(
2025-09-20 04:36:22 +07:00
'Mode Lokal - Data tersimpan di perangkat',
2025-09-20 03:10:05 +07:00
style: TextStyle(
color: Colors.white,
fontSize: 13,
fontWeight: FontWeight.w500,
),
),
),
2025-09-20 04:36:22 +07:00
// Only refresh button
2025-09-20 03:10:05 +07:00
InkWell(
2025-09-20 04:36:22 +07:00
onTap: _refreshData,
2025-09-20 03:10:05 +07:00
child: Container(
padding: EdgeInsets.all(4),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.2),
borderRadius: BorderRadius.circular(4),
),
child: Icon(Icons.refresh, color: Colors.white, size: 14),
),
),
],
),
);
}
2025-09-20 04:36:22 +07:00
Widget _buildCategoryLoadingState() {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(color: AppColors.primary),
SizedBox(height: 16),
Text(
'Memuat kategori...',
style: TextStyle(color: Colors.grey.shade600),
),
],
2025-09-20 03:10:05 +07:00
),
2025-09-20 04:36:22 +07:00
);
}
Widget _buildCategoryErrorState(String message) {
return Center(
2025-09-20 03:10:05 +07:00
child: Column(
2025-09-20 04:36:22 +07:00
mainAxisAlignment: MainAxisAlignment.center,
2025-09-20 03:10:05 +07:00
children: [
2025-09-20 04:36:22 +07:00
Icon(Icons.error_outline, size: 48, color: Colors.red.shade400),
SizedBox(height: 16),
Text('Error Kategori',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
SizedBox(height: 8),
Padding(
padding: EdgeInsets.symmetric(horizontal: 32),
child: Text(
message,
textAlign: TextAlign.center,
style: TextStyle(color: Colors.grey.shade600),
),
),
SizedBox(height: 16),
Button.filled(
width: 120,
onPressed: () {
context
.read<CategoryLoaderBloc>()
.add(const CategoryLoaderEvent.getCategories());
2025-09-20 03:10:05 +07:00
},
2025-09-20 04:36:22 +07:00
label: 'Coba Lagi',
2025-09-20 03:10:05 +07:00
),
2025-09-20 04:36:22 +07:00
],
),
);
}
2025-09-20 03:10:05 +07:00
2025-09-20 04:36:22 +07:00
Widget _buildCategoryContent(List<CategoryModel> categories) {
return Column(
children: [
// Simple home title
_buildSimpleHomeTitle(),
2025-09-20 03:10:05 +07:00
2025-09-20 04:36:22 +07:00
// Products section with categories
Expanded(
child: BlocBuilder<ProductLoaderBloc, ProductLoaderState>(
builder: (context, productState) {
return CategoryTabBar(
categories: categories,
tabViews: categories.map((category) {
return SizedBox(
child: productState.maybeWhen(
orElse: () => _buildLoadingState(),
loading: () => _buildLoadingState(),
loaded: (products, hasReachedMax, currentPage,
isLoadingMore, categoryId, searchQuery) {
if (products.isEmpty) {
return _buildEmptyState(categoryId);
}
return _buildProductGrid(
products,
hasReachedMax,
isLoadingMore,
categoryId,
currentPage,
);
},
error: (message) =>
_buildErrorState(message, category.id),
2025-08-05 15:25:10 +07:00
),
2025-09-20 04:36:22 +07:00
);
}).toList(),
);
},
),
),
],
2025-09-20 03:10:05 +07:00
);
}
2025-09-20 04:36:22 +07:00
// Simple home title
Widget _buildSimpleHomeTitle() {
2025-09-20 03:10:05 +07:00
return Container(
2025-09-20 04:36:22 +07:00
padding: EdgeInsets.all(16),
2025-09-20 03:10:05 +07:00
decoration: BoxDecoration(
2025-09-20 04:36:22 +07:00
color: Colors.white,
border: Border(bottom: BorderSide(color: Colors.grey.shade200)),
2025-09-20 03:10:05 +07:00
),
2025-09-20 04:36:22 +07:00
child: HomeTitle(
controller: searchController,
onChanged: (value) {
setState(() {
searchQuery = value;
});
// Fast local search
Future.delayed(Duration(milliseconds: 200), () {
if (value == searchController.text) {
log('🔍 Local search: "$value"');
context.read<ProductLoaderBloc>().add(
ProductLoaderEvent.searchProduct(
query: value,
),
);
}
});
},
2025-09-20 03:10:05 +07:00
),
);
}
Widget _buildLoadingState() {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(color: AppColors.primary),
SizedBox(height: 16),
Text(
2025-09-20 04:36:22 +07:00
'Memuat data...',
2025-09-20 03:10:05 +07:00
style: TextStyle(color: Colors.grey.shade600),
),
],
),
);
}
Widget _buildEmptyState(String? categoryId) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Assets.icons.noProduct.svg(),
SizedBox(height: 20),
Text(
searchQuery.isNotEmpty
? 'Produk "$searchQuery" tidak ditemukan'
2025-09-20 04:36:22 +07:00
: 'Belum ada data produk',
2025-09-20 03:10:05 +07:00
textAlign: TextAlign.center,
),
SizedBox(height: 8),
Text(
2025-09-20 04:36:22 +07:00
'Data akan dimuat dari database lokal',
2025-09-20 03:10:05 +07:00
style: TextStyle(
color: Colors.grey.shade600,
fontSize: 12,
),
textAlign: TextAlign.center,
),
SpaceHeight(20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (searchQuery.isNotEmpty) ...[
Button.filled(
width: 100,
onPressed: () {
searchController.clear();
setState(() => searchQuery = '');
context.read<ProductLoaderBloc>().add(
ProductLoaderEvent.getProduct(categoryId: categoryId),
);
},
label: 'Reset',
),
SizedBox(width: 12),
],
Button.filled(
width: 120,
onPressed: () {
context.read<ProductLoaderBloc>().add(
ProductLoaderEvent.getProduct(categoryId: categoryId),
);
},
label: 'Muat Ulang',
2025-07-30 22:38:44 +07:00
),
2025-08-05 15:25:10 +07:00
],
),
2025-09-20 03:10:05 +07:00
],
2025-07-30 22:38:44 +07:00
),
);
}
2025-09-20 03:10:05 +07:00
Widget _buildErrorState(String message, String? categoryId) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
2025-09-20 04:36:22 +07:00
Icon(Icons.error_outline, size: 48, color: Colors.red.shade400),
2025-09-20 03:10:05 +07:00
SizedBox(height: 16),
Text(
2025-09-20 04:36:22 +07:00
'Error Database',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
2025-09-20 03:10:05 +07:00
),
SizedBox(height: 8),
Padding(
padding: EdgeInsets.symmetric(horizontal: 32),
child: Text(
message,
textAlign: TextAlign.center,
style: TextStyle(color: Colors.grey.shade600),
),
),
SizedBox(height: 16),
Button.filled(
width: 120,
onPressed: () {
context.read<ProductLoaderBloc>().add(
ProductLoaderEvent.getProduct(categoryId: categoryId),
);
},
label: 'Coba Lagi',
),
],
),
);
}
2025-07-30 22:38:44 +07:00
2025-09-20 03:10:05 +07:00
Widget _buildProductGrid(
List products,
bool hasReachedMax,
bool isLoadingMore,
String? categoryId,
int currentPage,
) {
2025-07-30 22:38:44 +07:00
return Column(
children: [
2025-09-20 04:36:22 +07:00
// Simple product count
2025-09-20 03:10:05 +07:00
if (products.isNotEmpty)
Container(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 6),
child: Row(
children: [
Text(
2025-09-20 04:36:22 +07:00
'${products.length} produk ditemukan',
2025-09-20 03:10:05 +07:00
style: TextStyle(
color: Colors.grey.shade600,
2025-09-20 04:36:22 +07:00
fontSize: 12,
2025-09-20 03:10:05 +07:00
),
),
Spacer(),
if (isLoadingMore)
SizedBox(
width: 12,
height: 12,
child: CircularProgressIndicator(
strokeWidth: 2,
color: AppColors.primary,
),
),
],
),
),
2025-09-20 04:36:22 +07:00
// Products grid
2025-09-20 03:10:05 +07:00
Expanded(
child: NotificationListener<ScrollNotification>(
onNotification: (notification) =>
_handleScrollNotification(notification, categoryId),
child: GridView.builder(
itemCount: products.length,
controller: scrollController,
padding: const EdgeInsets.all(16),
2025-09-20 04:36:22 +07:00
cacheExtent: 200.0,
2025-09-20 03:10:05 +07:00
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 180,
mainAxisSpacing: 30,
crossAxisSpacing: 30,
childAspectRatio: 180 / 240,
),
itemBuilder: (context, index) => ProductCard(
data: products[index],
onCartButton: () {
// Cart functionality
},
),
),
),
2025-07-30 22:38:44 +07:00
),
2025-09-20 03:10:05 +07:00
2025-09-20 04:36:22 +07:00
// End indicator
2025-09-20 03:10:05 +07:00
if (hasReachedMax && products.isNotEmpty)
Container(
padding: EdgeInsets.all(8),
child: Text(
2025-09-20 04:36:22 +07:00
'Semua produk telah dimuat',
2025-09-20 03:10:05 +07:00
style: TextStyle(
color: Colors.grey.shade500,
fontSize: 11,
),
),
),
2025-07-30 22:38:44 +07:00
],
);
}
2025-09-20 03:10:05 +07:00
// Cart section (unchanged from original)
Widget _buildCartSection() {
return Align(
alignment: Alignment.topCenter,
child: Material(
color: Colors.white,
child: Column(
children: [
HomeRightTitle(table: widget.table),
Padding(
padding: const EdgeInsets.all(16.0).copyWith(bottom: 0, top: 27),
child: Column(
children: [
const Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Item',
style: TextStyle(
color: AppColors.primary,
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
SizedBox(width: 130),
SizedBox(
width: 50.0,
child: Text(
'Qty',
style: TextStyle(
color: AppColors.primary,
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
SizedBox(
child: Text(
'Price',
style: TextStyle(
color: AppColors.primary,
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
],
),
const SpaceHeight(8),
const Divider(),
],
),
),
Expanded(
child: BlocBuilder<CheckoutBloc, CheckoutState>(
builder: (context, state) {
return state.maybeWhen(
orElse: () => const Center(child: Text('No Items')),
loaded: (products,
discountModel,
discount,
discountAmount,
tax,
serviceCharge,
totalQuantity,
totalPrice,
draftName,
orderType,
deliveryType) {
if (products.isEmpty) {
return const Center(child: Text('No Items'));
}
return ListView.separated(
shrinkWrap: true,
padding: const EdgeInsets.symmetric(horizontal: 16),
itemBuilder: (context, index) =>
OrderMenu(data: products[index]),
separatorBuilder: (context, index) =>
const SpaceHeight(1.0),
itemCount: products.length,
);
},
);
},
),
),
2025-09-20 04:36:22 +07:00
// Payment section
2025-09-20 03:10:05 +07:00
Padding(
padding: const EdgeInsets.all(16.0).copyWith(top: 0),
child: Column(
children: [
const Divider(),
const SpaceHeight(16.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Pajak',
style: TextStyle(
color: AppColors.black,
fontWeight: FontWeight.bold,
),
),
BlocBuilder<CheckoutBloc, CheckoutState>(
builder: (context, state) {
final tax = state.maybeWhen(
orElse: () => 0,
loaded: (products,
discountModel,
discount,
discountAmount,
tax,
serviceCharge,
totalQuantity,
totalPrice,
draftName,
orderType,
deliveryType) {
if (products.isEmpty) return 0;
return tax;
},
);
return Text(
'$tax %',
style: const TextStyle(
color: AppColors.primary,
fontWeight: FontWeight.w600,
),
);
},
),
],
),
const SpaceHeight(16.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Sub total',
style: TextStyle(
color: AppColors.black,
fontWeight: FontWeight.bold,
),
),
BlocBuilder<CheckoutBloc, CheckoutState>(
builder: (context, state) {
final price = state.maybeWhen(
orElse: () => 0,
loaded: (products,
discountModel,
discount,
discountAmount,
tax,
serviceCharge,
totalQuantity,
totalPrice,
draftName,
orderType,
deliveryType) {
if (products.isEmpty) return 0;
return products
.map((e) =>
(e.product.price! * e.quantity) +
(e.variant?.priceModifier ?? 0))
.reduce((value, element) => value + element);
},
);
return Text(
price.currencyFormatRp,
style: const TextStyle(
color: AppColors.primary,
fontWeight: FontWeight.w900,
),
);
},
),
],
),
SpaceHeight(16.0),
BlocBuilder<CheckoutBloc, CheckoutState>(
builder: (context, state) {
return state.maybeWhen(
orElse: () => Align(
alignment: Alignment.bottomCenter,
child: Button.filled(
borderRadius: 12,
elevation: 1,
disabled: true,
onPressed: () {
context.push(ConfirmPaymentPage(
isTable: widget.table == null ? false : true,
table: widget.table,
));
},
label: 'Lanjutkan Pembayaran',
),
),
loaded: (items,
discountModel,
discount,
discountAmount,
tax,
serviceCharge,
totalQuantity,
totalPrice,
draftName,
orderType,
deliveryType) =>
Align(
alignment: Alignment.bottomCenter,
child: Button.filled(
borderRadius: 12,
elevation: 1,
disabled: items.isEmpty,
onPressed: () {
if (orderType.name == 'dineIn' &&
widget.table == null) {
AppFlushbar.showError(context,
'Mohon pilih meja terlebih dahulu');
return;
}
if (orderType.name == 'delivery' &&
deliveryType == null) {
AppFlushbar.showError(context,
'Mohon pilih pengiriman terlebih dahulu');
return;
}
context.push(ConfirmPaymentPage(
isTable: widget.table == null ? false : true,
table: widget.table,
));
},
label: 'Lanjutkan Pembayaran',
),
),
);
},
),
],
),
),
],
),
),
);
}
2025-07-30 22:38:44 +07:00
}