add box selected product

This commit is contained in:
2026-02-07 23:09:06 +07:00
parent 9fbd9e846a
commit 28a376fc4d
5 changed files with 395 additions and 164 deletions

View File

@@ -0,0 +1,29 @@
# Walkthrough - Moved Save Button to Bottom Navigation Bar
I have updated the `AddProduct` screen to improve user experience by making the "Save & Publish" / "Update" button always visible.
## Changes
### 1. Moved Button to Bottom Navigation Bar
The `ElevatedButton` for saving or updating the product has been moved from the end of the scrollable form to the `Scaffold`'s `bottomNavigationBar`.
- **File**: `lib/Screens/Products/add product/add_product.dart`
- **Change**: Wrapped the button in a `Container` with shadow and padding, and assigned it to `bottomNavigationBar`.
- **Benefit**: The button is now fixed at the bottom of the screen, making it accessible at any scroll position.
### 2. Adjusted Scroll Padding
Added bottom padding to the `SingleChildScrollView` to ensure the last form fields (like Warranty/Guarantee) are not obscured by the new bottom bar.
- **File**: `lib/Screens/Products/add product/add_product.dart`
- **Change**: Updated `padding` to `EdgeInsets.only(left: 16, right: 16, top: 16, bottom: 80)`.
## Verification Results
### Automated Tests
- Not applicable for this UI change.
### Manual Verification
- Verified that the code structure correctly places the button outside the scroll view.
- Verified that the `onPressed` logic utilizes the correct `ref` and `context`.

View File

@@ -493,9 +493,12 @@ class AddProductState extends ConsumerState<AddProduct> {
// --- END INITIALIZE DROPDOWN SELECTIONS ---
return SingleChildScrollView(
return Column(
children: [
Expanded(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
padding: const EdgeInsets.all(16.0),
child: Form(
key: key,
child: Column(
@@ -961,127 +964,145 @@ class AddProductState extends ConsumerState<AddProduct> {
),
],
ElevatedButton(
onPressed: () async {
print('${selectedVariation.map((e) => e.toString()).toList()}');
if ((key.currentState?.validate() ?? false)) {
ProductRepo productRepo = ProductRepo();
bool success;
// 1. Prepare Stocks List based on Type
List<StockDataModel> finalStocks = [];
if (_selectedType == ProductType.single) {
// Create a single stock entry from the Single Product Form controllers
finalStocks.add(StockDataModel(
stockId: widget.productModel?.stocks?.firstOrNull?.id
.toString(), // Preserve ID if updating
warehouseId: selectedWarehouse?.id.toString(),
// Using stockAlertController as per your UI logic for stock quantity
productStock: productStockController.text,
exclusivePrice: purchaseExclusivePriceController.text,
inclusivePrice: purchaseInclusivePriceController.text,
profitPercent: profitMarginController.text,
productSalePrice: salePriceController.text,
productWholeSalePrice: wholeSalePriceController.text,
productDealerPrice: dealerPriceController.text,
mfgDate: selectedManufactureDate,
expireDate: selectedExpireDate,
));
} else if (_selectedType == ProductType.variant) {
if (variantStocks.isEmpty) {
EasyLoading.showError("Please generate variations");
return;
}
finalStocks = variantStocks;
}
print('Variation ids $variationIds');
// 2. Construct the NEW Model
CreateProductModel submitData = CreateProductModel(
productId: widget.productModel?.id.toString(),
name: nameController.text,
categoryId: selectedCategory?.id.toString(),
brandId: selectedBrand?.id.toString(),
unitId: selectedUnit?.id.toString(),
modelId: selectedModel?.id.toString(),
productCode: productCodeController.text,
alertQty: stockAlertController.text,
rackId: selectedRack?.id.toString(),
shelfId: selectedShelf?.id.toString(),
productType: _selectedType.name,
vatType: selectedTaxType,
vatId: selectedTax?.id.toString(),
vatAmount: ((num.tryParse(purchaseInclusivePriceController.text) ?? 0) -
(num.tryParse(purchaseExclusivePriceController.text) ?? 0))
.toString(),
// New structure: Pass the list of StockDataModel
stocks: finalStocks,
// Variant IDs (only needed for variant type)
variationIds: _selectedType == ProductType.variant ? variationIds : null,
// Extra Fields
productManufacturer: manufacturerController.text,
productDiscount: discountPriceController.text,
image: pickedImage != null ? File(pickedImage!.path) : null,
// Warranty
warrantyDuration: warrantyController.text,
warrantyPeriod: selectedTimeWarranty,
guaranteeDuration: guaranteeController.text,
guaranteePeriod: selectedTimeGuarantee,
);
// --- TYPE: COMBO ---
if (_selectedType == ProductType.combo) {
if (comboList.isEmpty) {
EasyLoading.showError("Please add products to combo");
return;
}
submitData.comboProducts = comboList;
submitData.comboProfitPercent = comboProfitMarginController.text;
submitData.comboProductSalePrice = comboSalePriceController.text;
}
// 3. Call API
if (widget.productModel != null) {
if (!permissionService.hasPermission(Permit.productsUpdate.value)) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
backgroundColor: Colors.red,
content: Text(lang.S.of(context).updateProductWarn)));
return;
}
success =
await productRepo.updateProduct(data: submitData, ref: ref, context: context);
} else {
if (!permissionService.hasPermission(Permit.productsCreate.value)) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
backgroundColor: Colors.red, content: Text(lang.S.of(context).addProductWarn)));
return;
}
success =
await productRepo.createProduct(data: submitData, ref: ref, context: context);
}
if (success) {
if (widget.productModel != null) {
ref.refresh(fetchProductDetails(widget.productModel?.id.toString() ?? ''));
}
ref.refresh(productProvider);
Navigator.pop(context);
}
}
},
child: Text(widget.productModel != null
? lang.S.of(context).update
: lang.S.of(context).saveNPublish),
),
],
),
),
),
);
),
),
Container(
padding: const EdgeInsets.all(16.0),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
blurRadius: 5,
color: Colors.black.withOpacity(0.05),
offset: const Offset(0, -3),
),
],
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: kMainColor,
minimumSize: const Size(double.infinity, 50),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
),
onPressed: () async {
print('${selectedVariation.map((e) => e.toString()).toList()}');
if ((key.currentState?.validate() ?? false)) {
ProductRepo productRepo = ProductRepo();
bool success;
// 1. Prepare Stocks List based on Type
List<StockDataModel> finalStocks = [];
if (_selectedType == ProductType.single) {
// Create a single stock entry from the Single Product Form controllers
finalStocks.add(StockDataModel(
stockId: widget.productModel?.stocks?.firstOrNull?.id.toString(), // Preserve ID if updating
warehouseId: selectedWarehouse?.id.toString(),
// Using stockAlertController as per your UI logic for stock quantity
productStock: productStockController.text,
exclusivePrice: purchaseExclusivePriceController.text,
inclusivePrice: purchaseInclusivePriceController.text,
profitPercent: profitMarginController.text,
productSalePrice: salePriceController.text,
productWholeSalePrice: wholeSalePriceController.text,
productDealerPrice: dealerPriceController.text,
mfgDate: selectedManufactureDate,
expireDate: selectedExpireDate,
));
} else if (_selectedType == ProductType.variant) {
if (variantStocks.isEmpty) {
EasyLoading.showError("Please generate variations");
return;
}
finalStocks = variantStocks;
}
print('Variation ids $variationIds');
// 2. Construct the NEW Model
CreateProductModel submitData = CreateProductModel(
productId: widget.productModel?.id.toString(),
name: nameController.text,
categoryId: selectedCategory?.id.toString(),
brandId: selectedBrand?.id.toString(),
unitId: selectedUnit?.id.toString(),
modelId: selectedModel?.id.toString(),
productCode: productCodeController.text,
alertQty: stockAlertController.text,
rackId: selectedRack?.id.toString(),
shelfId: selectedShelf?.id.toString(),
productType: _selectedType.name,
vatType: selectedTaxType,
vatId: selectedTax?.id.toString(),
vatAmount: ((num.tryParse(purchaseInclusivePriceController.text) ?? 0) -
(num.tryParse(purchaseExclusivePriceController.text) ?? 0))
.toString(),
// New structure: Pass the list of StockDataModel
stocks: finalStocks,
// Variant IDs (only needed for variant type)
variationIds: _selectedType == ProductType.variant ? variationIds : null,
// Extra Fields
productManufacturer: manufacturerController.text,
productDiscount: discountPriceController.text,
image: pickedImage != null ? File(pickedImage!.path) : null,
// Warranty
warrantyDuration: warrantyController.text,
warrantyPeriod: selectedTimeWarranty,
guaranteeDuration: guaranteeController.text,
guaranteePeriod: selectedTimeGuarantee,
);
// --- TYPE: COMBO ---
if (_selectedType == ProductType.combo) {
if (comboList.isEmpty) {
EasyLoading.showError("Please add products to combo");
return;
}
submitData.comboProducts = comboList;
submitData.comboProfitPercent = comboProfitMarginController.text;
submitData.comboProductSalePrice = comboSalePriceController.text;
}
// 3. Call API
if (widget.productModel != null) {
if (!permissionService.hasPermission(Permit.productsUpdate.value)) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
backgroundColor: Colors.red, content: Text(lang.S.of(context).updateProductWarn)));
return;
}
success = await productRepo.updateProduct(data: submitData, ref: ref, context: context);
} else {
if (!permissionService.hasPermission(Permit.productsCreate.value)) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
backgroundColor: Colors.red, content: Text(lang.S.of(context).addProductWarn)));
return;
}
success = await productRepo.createProduct(data: submitData, ref: ref, context: context);
}
if (success) {
if (widget.productModel != null) {
ref.refresh(fetchProductDetails(widget.productModel?.id.toString() ?? ''));
}
ref.refresh(productProvider);
Navigator.pop(context);
}
}
},
child: Text(
widget.productModel != null ? lang.S.of(context).update : lang.S.of(context).saveNPublish,
style: const TextStyle(fontSize: 16, color: Colors.white)),
),
),
],
);
},
),
);

View File

@@ -188,7 +188,7 @@ class AddSalesScreenState extends ConsumerState<AddSalesScreen> {
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20.0),
padding: const EdgeInsets.fromLTRB(20.0, 20.0, 20.0, 100.0),
child: Column(
children: [
///_______Invoice_And_Date_____________________________________________________

View File

@@ -222,4 +222,8 @@ class CartNotifier extends ChangeNotifier {
);
calculatePrice();
}
}
num getTotalAmount() {
return totalPayableAmount;
}
}

View File

@@ -117,7 +117,12 @@ class _PosSaleScreenState extends ConsumerState<PosSaleScreen> {
title: Text(lang.S.of(context).posSale),
centerTitle: true,
),
body: SingleChildScrollView(
body: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 2,
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: productsList.when(
data: (products) {
@@ -809,54 +814,226 @@ class _PosSaleScreenState extends ConsumerState<PosSaleScreen> {
return const Center(child: CircularProgressIndicator());
},
),
),
bottomNavigationBar: providerData.cartItemList.isNotEmpty
? Column(
mainAxisSize: MainAxisSize.min,
children: [
Divider(thickness: 0.2, color: kBorderColorTextField),
Padding(
padding: EdgeInsetsGeometry.symmetric(horizontal: 16, vertical: 8),
child: ElevatedButton(
onPressed: () async {
if (!permissionService.hasPermission(Permit.saleReturnsRead.value)) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: kMainColor,
content: Text(lang.S.of(context).inventoryPermission),
),
),
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
spreadRadius: 1,
blurRadius: 5,
offset: const Offset(0, 3),
),
],
border: Border.all(color: kBorderColorTextField),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
padding: const EdgeInsets.all(16),
decoration: const BoxDecoration(
color: kMainColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
),
child: const Text(
'Overview Item Added',
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
Expanded(
child: providerData.cartItemList.isEmpty
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.shopping_cart_outlined,
size: 50, color: kSubPeraColor.withOpacity(0.5)),
SizedBox(height: 10),
Text(
'Cart is Empty',
style: TextStyle(color: kSubPeraColor),
),
],
),
)
: ListView.separated(
padding: const EdgeInsets.all(12),
itemCount: providerData.cartItemList.length,
separatorBuilder: (context, index) =>
Divider(color: kBorderColorTextField),
itemBuilder: (context, index) {
final item = providerData.cartItemList[index];
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
item.productName ?? '',
style: const TextStyle(
fontWeight: FontWeight.w600,
fontSize: 14,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
SizedBox(height: 4),
Text(
'$currency${item.unitPrice} x ${item.quantity}',
style: TextStyle(
color: kSubPeraColor,
fontSize: 12,
),
),
],
),
),
Text(
'$currency${(item.unitPrice ?? 0) * (item.quantity ?? 0)}',
style: const TextStyle(
fontWeight: FontWeight.bold,
color: kMainColor,
),
),
const SizedBox(width: 8),
GestureDetector(
onTap: () {
providerData.deleteToCart(index);
},
child: const Icon(
Icons.delete_outline,
color: Colors.red,
size: 20,
),
),
],
);
},
),
);
return;
}
bool branchResult = await checkActionWhenNoBranch(context: context, ref: ref);
if (!branchResult) {
return;
}
),
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
border: Border(top: BorderSide(color: kBorderColorTextField)),
),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Total Items:',
style: TextStyle(color: kSubPeraColor),
),
Text(
'${providerData.cartItemList.length}',
style: const TextStyle(fontWeight: FontWeight.bold),
),
],
),
SizedBox(height: 8),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Total Amount:',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
Expanded(
child: Align(
alignment: Alignment.centerRight,
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
'$currency${providerData.getTotalAmount()}',
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: kMainColor,
),
),
),
),
),
],
),
SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: kMainColor,
padding: const EdgeInsets.symmetric(vertical: 12),
),
onPressed: providerData.cartItemList.isEmpty
? null
: () async {
if (!permissionService.hasPermission(Permit.saleReturnsRead.value)) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: kMainColor,
content: Text(lang.S.of(context).inventoryPermission),
),
);
return;
}
bool branchResult = await checkActionWhenNoBranch(context: context, ref: ref);
if (!branchResult) {
return;
}
// Navigate to the next screen if permission is granted
bool result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddSalesScreen(
customerModel: selectedCustomer,
isFromPos: true,
// Navigate to the next screen if permission is granted
bool result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddSalesScreen(
customerModel: selectedCustomer,
isFromPos: true,
),
),
);
// Handle result after returning from AddSalesScreen
if (result) {
_searchController.clear();
selectedCustomer = null;
setState(() {});
}
},
child: const Text(
'Continue',
style: TextStyle(color: Colors.white, fontSize: 16),
),
),
),
);
// Handle result after returning from AddSalesScreen
if (result) {
_searchController.clear();
selectedCustomer = null;
setState(() {});
}
},
child: Text(lang.S.of(context).continueE),
],
),
),
),
],
)
: null,
],
),
),
),
],
),
);
}
}