170 lines
5.8 KiB
Dart
Raw Normal View History

2025-07-30 22:38:44 +07:00
import 'package:cached_network_image/cached_network_image.dart';
2025-08-03 00:35:00 +07:00
import 'package:enaklo_pos/core/extensions/int_ext.dart';
2025-08-04 17:42:39 +07:00
import 'package:enaklo_pos/presentation/home/dialog/variant_dialog.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/constants/variables.dart';
import 'package:enaklo_pos/data/models/response/product_response_model.dart';
import 'package:enaklo_pos/presentation/home/bloc/checkout/checkout_bloc.dart';
import '../../../core/constants/colors.dart';
class ProductCard extends StatelessWidget {
final Product data;
final VoidCallback onCartButton;
2025-07-31 23:22:34 +07:00
const ProductCard({
2025-07-30 22:38:44 +07:00
super.key,
required this.data,
required this.onCartButton,
});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
2025-08-04 17:42:39 +07:00
if (data.variants!.isEmpty) {
context.read<CheckoutBloc>().add(
CheckoutEvent.addItem(data, null),
);
} else {
showDialog(
context: context,
builder: (context) => VariantDialog(product: data),
);
}
2025-07-30 22:38:44 +07:00
},
child: Container(
2025-07-31 19:25:45 +07:00
decoration: BoxDecoration(
color: AppColors.white,
2025-08-02 11:00:30 +07:00
borderRadius: BorderRadius.circular(8.0),
2025-07-31 23:22:34 +07:00
border: Border.all(
color: AppColors.disabled,
),
2025-07-30 22:38:44 +07:00
),
child: Stack(
children: [
2025-08-02 11:00:30 +07:00
Padding(
padding: const EdgeInsets.all(4.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
2025-08-01 00:46:22 +07:00
child: CachedNetworkImage(
2025-08-03 01:00:33 +07:00
imageUrl: (data.imageUrl ?? "").contains('http')
? data.imageUrl!
: '${Variables.baseUrl}/${data.imageUrl}',
2025-08-01 00:46:22 +07:00
fit: BoxFit.cover,
2025-08-02 11:00:30 +07:00
width: double.infinity,
height: 120,
2025-08-01 00:46:22 +07:00
errorWidget: (context, url, error) => Container(
2025-08-02 11:00:30 +07:00
width: double.infinity,
height: 120,
2025-08-01 00:46:22 +07:00
decoration: BoxDecoration(
color: AppColors.disabled.withOpacity(0.4),
),
child: const Icon(
2025-08-02 11:00:30 +07:00
Icons.image,
2025-08-01 00:46:22 +07:00
color: AppColors.grey,
),
),
2025-07-30 22:38:44 +07:00
),
),
2025-08-02 11:00:30 +07:00
const Spacer(),
Text(
"${data.name}",
2025-07-31 23:22:34 +07:00
style: const TextStyle(
fontSize: 14,
2025-08-02 11:00:30 +07:00
fontWeight: FontWeight.w700,
2025-07-30 22:38:44 +07:00
),
2025-08-02 11:00:30 +07:00
maxLines: 1,
2025-07-31 23:22:34 +07:00
overflow: TextOverflow.ellipsis,
),
2025-08-02 11:00:30 +07:00
const Spacer(),
Align(
alignment: Alignment.center,
child: Text(
2025-08-03 00:35:00 +07:00
'-',
2025-08-02 11:00:30 +07:00
style: const TextStyle(
fontSize: 12,
color: AppColors.grey,
fontWeight: FontWeight.w500,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
const Spacer(),
Align(
alignment: Alignment.center,
child: Text(
2025-08-03 00:35:00 +07:00
data.price!.currencyFormatRp,
2025-08-02 11:00:30 +07:00
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 12,
),
2025-07-30 22:38:44 +07:00
),
2025-07-31 23:22:34 +07:00
),
2025-08-02 11:00:30 +07:00
const Spacer(),
],
),
2025-07-30 22:38:44 +07:00
),
BlocBuilder<CheckoutBloc, CheckoutState>(
builder: (context, state) {
return state.maybeWhen(
orElse: () => const SizedBox(),
2025-08-06 18:47:20 +07:00
loaded: (
products,
discountModel,
discount,
discountAmount,
tax,
serviceCharge,
totalQuantity,
totalPrice,
draftName,
orderType,
deliveryType,
) {
2025-08-04 17:42:39 +07:00
final totalQuantity = products
.where((item) => item.product.id == data.id)
.map((item) => item.quantity)
.fold(0, (sum, qty) => sum + qty);
if (totalQuantity == 0) {
return const SizedBox.shrink();
}
return Align(
alignment: Alignment.topRight,
child: Container(
width: 40,
height: 40,
padding: const EdgeInsets.all(6),
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(9.0)),
color: AppColors.primary,
),
child: Center(
child: Text(
totalQuantity.toString(),
style: const TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold),
),
),
),
);
2025-07-30 22:38:44 +07:00
},
);
},
),
],
),
),
);
}
}