feat: update item quantity to support double values

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
master v1.1
a2nr 2025-09-18 11:39:03 +07:00
parent b3cb5e3e05
commit 77b3f5bb79
2 changed files with 16 additions and 11 deletions

View File

@ -1,6 +1,6 @@
class ReceiptItem { class ReceiptItem {
final String description; final String description;
final int quantity; final double quantity;
final double price; final double price;
ReceiptItem({ ReceiptItem({
@ -15,4 +15,5 @@ class ReceiptItem {
String toString() { String toString() {
return 'ReceiptItem(description: $description, quantity: $quantity, price: $price)'; return 'ReceiptItem(description: $description, quantity: $quantity, price: $price)';
} }
} }

View File

@ -3,7 +3,7 @@ import 'package:cashumit/models/receipt_item.dart';
class AddItemScreen extends StatefulWidget { class AddItemScreen extends StatefulWidget {
const AddItemScreen({super.key, this.item}); const AddItemScreen({super.key, this.item});
// Constructor untuk mode edit // Constructor untuk mode edit
const AddItemScreen.fromItem(this.item, {super.key}); const AddItemScreen.fromItem(this.item, {super.key});
@ -22,11 +22,14 @@ class _AddItemScreenState extends State<AddItemScreen> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
// Inisialisasi controller dengan nilai default atau dari item yang diedit // Inisialisasi controller dengan nilai default atau dari item yang diedit
_descriptionController = TextEditingController(text: widget.item?.description ?? ''); _descriptionController =
_quantityController = TextEditingController(text: widget.item != null ? widget.item!.quantity.toString() : '1'); TextEditingController(text: widget.item?.description ?? '');
_priceController = TextEditingController(text: widget.item?.price.toString() ?? ''); _quantityController = TextEditingController(
text: widget.item != null ? widget.item!.quantity.toString() : '1');
_priceController =
TextEditingController(text: widget.item?.price.toString() ?? '');
} }
@override @override
@ -41,10 +44,10 @@ class _AddItemScreenState extends State<AddItemScreen> {
if (_formKey.currentState!.validate()) { if (_formKey.currentState!.validate()) {
final item = ReceiptItem( final item = ReceiptItem(
description: _descriptionController.text, description: _descriptionController.text,
quantity: int.parse(_quantityController.text), quantity: double.parse(_quantityController.text),
price: double.parse(_priceController.text), price: double.parse(_priceController.text),
); );
Navigator.pop(context, item); Navigator.pop(context, item);
} }
} }
@ -87,7 +90,7 @@ class _AddItemScreenState extends State<AddItemScreen> {
if (value == null || value.isEmpty) { if (value == null || value.isEmpty) {
return 'Mohon masukkan jumlah'; return 'Mohon masukkan jumlah';
} }
if (int.tryParse(value) == null) { if (double.tryParse(value) == null) {
return 'Jumlah harus berupa angka'; return 'Jumlah harus berupa angka';
} }
return null; return null;
@ -132,4 +135,5 @@ class _AddItemScreenState extends State<AddItemScreen> {
], ],
); );
} }
} }