Validations on add product screen

This commit is contained in:
kratikpal 2024-08-16 16:36:18 +05:30
parent aa04f39fca
commit 7cce7db73c
4 changed files with 52 additions and 22 deletions

View File

@ -2,13 +2,13 @@ class ProductResponse {
final bool success;
final int totalData;
final int totalPages;
final List<Product> product;
final List<Product> products;
ProductResponse({
required this.success,
required this.totalData,
required this.totalPages,
required this.product,
required this.products,
});
factory ProductResponse.fromJson(Map<String, dynamic> json) {
@ -16,7 +16,7 @@ class ProductResponse {
success: json['success'],
totalData: json['total_data'],
totalPages: json['total_pages'],
product: (json['product'] as List)
products: (json['products'] as List)
.map((item) => Product.fromJson(item))
.toList(),
);
@ -27,7 +27,7 @@ class ProductResponse {
'success': success,
'total_data': totalData,
'total_pages': totalPages,
'product': product.map((item) => item.toJson()).toList(),
'products': products.map((item) => item.toJson()).toList(),
};
}
}
@ -85,6 +85,8 @@ class Product {
createdAt: DateTime.parse(json['createdAt']),
updatedAt: DateTime.parse(json['updatedAt']),
v: json['__v'],
sale: json['sale'], // Nullable field
inventory: json['inventory'], // Nullable field
);
}
@ -104,6 +106,8 @@ class Product {
'createdAt': createdAt.toIso8601String(),
'updatedAt': updatedAt.toIso8601String(),
'__v': v,
'sale': sale, // Nullable field
'inventory': inventory, // Nullable field
};
}
}

View File

@ -32,7 +32,7 @@ class ProductProvider extends ChangeNotifier {
setLoading(false);
if (response.statusCode == 200) {
productResponse = ProductResponse.fromJson(response.data);
productList = productResponse!.product;
productList = productResponse!.products;
notifyListeners();
}
} catch (e) {

View File

@ -1,3 +1,4 @@
import 'package:cheminova/models/pd_rd_response_model.dart';
import 'package:cheminova/models/product_model.dart';
import 'package:cheminova/provider/product_provider.dart';
import 'package:cheminova/screens/data_submit_successfull.dart';
@ -9,7 +10,13 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class AddProductsScreen extends StatefulWidget {
const AddProductsScreen({super.key});
final PdRdResponseModel distributor;
final String distributorType;
const AddProductsScreen({
super.key,
required this.distributor,
required this.distributorType,
});
@override
State<AddProductsScreen> createState() => _AddProductsScreenState();
@ -64,9 +71,9 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
padding: const EdgeInsets.only(right: 20),
),
],
title: const Text(
'Add Products',
style: TextStyle(
title: Text(
widget.distributor.name,
style: const TextStyle(
fontSize: 20,
color: Colors.black,
fontWeight: FontWeight.w400,
@ -215,7 +222,7 @@ class _AddProductsScreenState extends State<AddProductsScreen> {
provider
.submitSelectedProducts(
"PrincipalDistributor",
"66a0e19c981736b70ed4e34e")
widget.distributor.id)
.then((value) {
if (value) {
Navigator.push(
@ -268,17 +275,27 @@ class _ProductBlockState extends State<ProductBlock> {
setState(() {
if (saleController.text.isNotEmpty &&
inventoryController.text.isNotEmpty) {
// Check if the input can be parsed as an integer
if (int.tryParse(saleController.text) == null ||
int.tryParse(inventoryController.text) == null) {
errorMessage = 'Please enter valid integer value';
} else {
// Parse the input as integers
int sale = int.parse(saleController.text);
int inventory = int.parse(inventoryController.text);
// Validate if inventory is less than or equal to sales
if (inventory > sale) {
errorMessage = 'Inventory should be less than or equal to sales';
} else {
errorMessage = null;
}
}
} else {
errorMessage = null;
errorMessage = 'Please fill in both fields';
}
});
return errorMessage == null;
}

View File

@ -65,12 +65,21 @@ class _UpdateInventoryScreenState extends State<UpdateInventoryScreen> {
text: 'SUBMIT',
backgroundColor: const Color(0xff004791),
onPressed: () {
if(selectedDistributor == null || selectedDistributorType == null){
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please select distributor type and distributor')),
);
}else{
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const AddProductsScreen(),
builder: (context) => AddProductsScreen(
distributor: selectedDistributor!,
distributorType: selectedDistributorType!,
),
),
);
}
},
),
),