fix force close at home

This commit is contained in:
efrilm 2025-09-19 13:01:28 +07:00
parent 78914d7140
commit f18a70b312

View File

@ -14,7 +14,6 @@ import 'package:enaklo_pos/core/extensions/build_context_ext.dart';
import 'package:enaklo_pos/core/extensions/int_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/data/models/response/table_model.dart';
import 'package:enaklo_pos/presentation/home/pages/confirm_payment_page.dart'; import 'package:enaklo_pos/presentation/home/pages/confirm_payment_page.dart';
import 'package:enaklo_pos/data/models/response/product_response_model.dart';
import '../../../core/assets/assets.gen.dart'; import '../../../core/assets/assets.gen.dart';
import '../../../core/components/buttons.dart'; import '../../../core/components/buttons.dart';
@ -94,25 +93,8 @@ class _HomePageState extends State<HomePage> {
}); });
} }
List<Product> _filterProducts(List<Product> products) {
if (searchQuery.isEmpty) {
return products;
}
return products.where((product) {
final productName = product.name?.toLowerCase() ?? '';
final queryLower = searchQuery.toLowerCase();
return productName.contains(queryLower);
}).toList();
}
bool _handleScrollNotification( bool _handleScrollNotification(
ScrollNotification notification, String? categoryId) { ScrollNotification notification, String? categoryId) {
// Check if the ScrollController is attached before accessing position
if (!scrollController.hasClients) {
return false;
}
if (notification is ScrollEndNotification && if (notification is ScrollEndNotification &&
scrollController.position.extentAfter == 0) { scrollController.position.extentAfter == 0) {
context.read<ProductLoaderBloc>().add( context.read<ProductLoaderBloc>().add(
@ -181,62 +163,58 @@ class _HomePageState extends State<HomePage> {
), ),
BlocBuilder<ProductLoaderBloc, ProductLoaderState>( BlocBuilder<ProductLoaderBloc, ProductLoaderState>(
builder: (context, state) { builder: (context, state) {
return NotificationListener<ScrollNotification>( return Expanded(
onNotification: (notification) { child: CategoryTabBar(
return state.maybeWhen( categories: categories,
orElse: () => false, tabViews: categories.map((category) {
loaded: (products, hasReachedMax, return SizedBox(
currentPage, isLoadingMore) { child: state.maybeWhen(orElse: () {
return _handleScrollNotification( return const Center(
notification, categoryId); child: CircularProgressIndicator(),
}, );
); }, loading: () {
}, return const Center(
child: Expanded( child: CircularProgressIndicator(),
child: CategoryTabBar( );
categories: categories, }, loaded: (products, hashasReachedMax,
tabViews: categories.map((category) { currentPage, isLoadingMore) {
return SizedBox( if (products.isEmpty) {
child: state.maybeWhen(orElse: () { return Center(
return const Center( child: Column(
child: children: [
CircularProgressIndicator(), Text('No Items Found'),
SpaceHeight(20),
Button.filled(
width: 120,
onPressed: () {
context
.read<
ProductLoaderBloc>()
.add(const ProductLoaderEvent
.getProduct());
},
label: 'Retry',
),
],
),
); );
}, loading: () { }
return const Center( return NotificationListener<
child: ScrollNotification>(
CircularProgressIndicator(), onNotification: (notification) {
); return state.maybeWhen(
}, loaded: (products, orElse: () => false,
hashasReachedMax, loaded: (products,
currentPage, hasReachedMax,
isLoadingMore) { currentPage,
final filteredProducts = isLoadingMore) {
_filterProducts(products); return _handleScrollNotification(
if (filteredProducts.isEmpty) { notification, categoryId);
return Center( },
child: Column(
children: [
Text('No Items Found'),
SpaceHeight(20),
Button.filled(
width: 120,
onPressed: () {
context
.read<
ProductLoaderBloc>()
.add(const ProductLoaderEvent
.getProduct());
},
label: 'Retry',
),
],
),
); );
} },
return GridView.builder( child: GridView.builder(
itemCount: itemCount: products.length,
filteredProducts.length,
controller: scrollController, controller: scrollController,
padding: const EdgeInsets.all(16), padding: const EdgeInsets.all(16),
gridDelegate: gridDelegate:
@ -248,14 +226,14 @@ class _HomePageState extends State<HomePage> {
), ),
itemBuilder: (context, index) => itemBuilder: (context, index) =>
ProductCard( ProductCard(
data: filteredProducts[index], data: products[index],
onCartButton: () {}, onCartButton: () {},
), ),
); ),
}), );
); }),
}).toList(), );
), }).toList(),
), ),
); );
}, },