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/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<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(
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<ProductLoaderBloc>().add(
@ -181,62 +163,58 @@ class _HomePageState extends State<HomePage> {
),
BlocBuilder<ProductLoaderBloc, ProductLoaderState>(
builder: (context, state) {
return NotificationListener<ScrollNotification>(
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<HomePage> {
),
itemBuilder: (context, index) =>
ProductCard(
data: filteredProducts[index],
data: products[index],
onCartButton: () {},
),
);
}),
);
}).toList(),
),
),
);
}),
);
}).toList(),
),
);
},