I'm making a point of sales application where there will be a list of items to be purchased along with the number of items and prices. but I'm having problems in making the total calculation.
this is my code to add data to List
final List<Product> listProduct;
widget.listProduct.add(data);
this is my model
Product productFromJson(String str) => Product.fromJson(json.decode(str));
String productToJson(Product data) => json.encode(data.toJson());
class Product {
final int id;
final int categoryId;
final String productName;
final int productQuantity;
final int productCost;
final int productPrice;
final String image;
int quantity = 1;
int total = 0;
final DateTime createdAt;
final DateTime updatedAt;
Product({
required this.id,
required this.categoryId,
required this.productName,
required this.productQuantity,
required this.productCost,
required this.productPrice,
required this.image,
required this.createdAt,
required this.updatedAt,
});
factory Product.fromJson(Map<String, dynamic> json) => Product(
id: json["id"],
categoryId: json["category_id"],
productName: json["product_name"],
productQuantity: json["product_quantity"],
productCost: json["product_cost"],
productPrice: json["product_price"],
image: json['image'],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"category_id": categoryId,
"product_name": productName,
"product_quantity": productQuantity,
"product_cost": productCost,
"product_price": productPrice,
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt.toIso8601String(),
};
}
and i user DataTable to display list data
DataTable(
columns: const <DataColumn>[
DataColumn(label: Text('No',style: TextStyle(fontStyle: FontStyle.italic))),
DataColumn(label: Text('Product Name',style: TextStyle(fontStyle: FontStyle.italic),),),
DataColumn(label: Text('Price',style: TextStyle(fontStyle: FontStyle.italic),),),
DataColumn(label: Text('Amount',style: TextStyle(fontStyle: FontStyle.italic),),),
DataColumn(label: Text('Total',style: TextStyle(fontStyle: FontStyle.italic),),),
DataColumn(label: Text('Delete',style: TextStyle(fontStyle: FontStyle.italic),),),
],
rows: List.generate(widget.listProduct.length, (index) {
Product dataProduct = widget.listProduct[index];
int total = dataProduct.quantity * dataProduct.productPrice;
String totalHarga = total.toString();
return DataRow(
cells: <DataCell>[
DataCell(Text('${index + 1}.')),
DataCell(Text(dataProduct.productName)),
DataCell(Text(dataProduct.productPrice.toString())),
DataCell(Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
onPressed: () {
setState(() {
// quantity--;
if (dataProduct.quantity >= 2) {
dataProduct.quantity--;
total = dataProduct.quantity *
dataProduct.productPrice;
} else {
return;
}
});
},
icon: Icon(Icons.remove)),
Text(dataProduct.quantity.toString()),
IconButton(
onPressed: () {
setState(() {
dataProduct.quantity++;
total = dataProduct.quantity *
dataProduct.productPrice;
print(total);
});
},
icon: Icon(Icons.add)),
],
)),
DataCell(Text(totalHarga)),
DataCell(
Icon(Icons.delete),
onTap: () {
widget.listProduct.remove(dataProduct);
getProductData();
},
)
],
);
}
),
),
use this method:
double totalPrice(){
double total = 0.0;
for(Product product in productList){
total += product.price * product.quantity;
}
return total;
}