From f18a70b312578ee6db9520e4a79ff4dea1fc149b Mon Sep 17 00:00:00 2001 From: efrilm Date: Fri, 19 Sep 2025 13:01:28 +0700 Subject: [PATCH] fix force close at home --- lib/presentation/home/pages/home_page.dart | 134 +++++++++------------ 1 file changed, 56 insertions(+), 78 deletions(-) diff --git a/lib/presentation/home/pages/home_page.dart b/lib/presentation/home/pages/home_page.dart index 569cc81..db6ca41 100644 --- a/lib/presentation/home/pages/home_page.dart +++ b/lib/presentation/home/pages/home_page.dart @@ -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/data/models/response/table_model.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/components/buttons.dart'; @@ -94,25 +93,8 @@ class _HomePageState extends State { }); } - List _filterProducts(List 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( ScrollNotification notification, String? categoryId) { - // Check if the ScrollController is attached before accessing position - if (!scrollController.hasClients) { - return false; - } - if (notification is ScrollEndNotification && scrollController.position.extentAfter == 0) { context.read().add( @@ -181,62 +163,58 @@ class _HomePageState extends State { ), BlocBuilder( builder: (context, state) { - return NotificationListener( - onNotification: (notification) { - return state.maybeWhen( - orElse: () => false, - loaded: (products, hasReachedMax, - currentPage, isLoadingMore) { - return _handleScrollNotification( - notification, categoryId); - }, - ); - }, - child: Expanded( - child: CategoryTabBar( - categories: categories, - tabViews: categories.map((category) { - return SizedBox( - child: state.maybeWhen(orElse: () { - return const Center( - child: - CircularProgressIndicator(), + return Expanded( + child: CategoryTabBar( + categories: categories, + tabViews: categories.map((category) { + return SizedBox( + child: state.maybeWhen(orElse: () { + return const Center( + child: CircularProgressIndicator(), + ); + }, loading: () { + return const Center( + child: CircularProgressIndicator(), + ); + }, loaded: (products, hashasReachedMax, + currentPage, isLoadingMore) { + if (products.isEmpty) { + 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', + ), + ], + ), ); - }, loading: () { - return const Center( - child: - CircularProgressIndicator(), - ); - }, loaded: (products, - hashasReachedMax, - currentPage, - isLoadingMore) { - final filteredProducts = - _filterProducts(products); - if (filteredProducts.isEmpty) { - 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 NotificationListener< + ScrollNotification>( + onNotification: (notification) { + return state.maybeWhen( + orElse: () => false, + loaded: (products, + hasReachedMax, + currentPage, + isLoadingMore) { + return _handleScrollNotification( + notification, categoryId); + }, ); - } - return GridView.builder( - itemCount: - filteredProducts.length, + }, + child: GridView.builder( + itemCount: products.length, controller: scrollController, padding: const EdgeInsets.all(16), gridDelegate: @@ -248,14 +226,14 @@ class _HomePageState extends State { ), itemBuilder: (context, index) => ProductCard( - data: filteredProducts[index], + data: products[index], onCartButton: () {}, ), - ); - }), - ); - }).toList(), - ), + ), + ); + }), + ); + }).toList(), ), ); },