From 40c31415a47d4997e02a11ed81c7a96a9e62d830 Mon Sep 17 00:00:00 2001 From: Adam Stolarczyk Date: Mon, 15 Jul 2024 10:36:52 +0200 Subject: [PATCH] fixes review/13 --- lib/barcode_item_widget.dart | 1 + lib/barcode_type.dart | 9 +++++++++ lib/barcodes_panel.dart | 15 ++++----------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/barcode_item_widget.dart b/lib/barcode_item_widget.dart index 404ea91..c4c8bcd 100644 --- a/lib/barcode_item_widget.dart +++ b/lib/barcode_item_widget.dart @@ -38,3 +38,4 @@ class BarcodeItemWidget extends StatelessWidget { ); } } + diff --git a/lib/barcode_type.dart b/lib/barcode_type.dart index 65f6e45..b362fcf 100644 --- a/lib/barcode_type.dart +++ b/lib/barcode_type.dart @@ -23,4 +23,13 @@ extension BarcodeTypeExtension on BarcodeType { return Barcode.ean8(); } } + + bool isValid(String data) { + switch (this) { + case BarcodeType.ean13: + return data.length == 13 && RegExp(r'^\d+$').hasMatch(data); + case BarcodeType.ean8: + return data.length == 8 && RegExp(r'^\d+$').hasMatch(data); + } + } } diff --git a/lib/barcodes_panel.dart b/lib/barcodes_panel.dart index de921f7..387f530 100644 --- a/lib/barcodes_panel.dart +++ b/lib/barcodes_panel.dart @@ -32,25 +32,18 @@ class BarcodesPanelState extends State { void _handleAddBarcode() { final description = _descriptionController.text; final data = _dataController.text; - final barcodeType = _selectedBarcodeType.barcode; + final barcodeType = _selectedBarcodeType; setState(() { _descriptionError = description.isEmpty ? "Opis nie może być pusty" : null; _dataError = data.isEmpty ? "Kod kreskowy nie może być pusty" : null; if (_descriptionError == null && _dataError == null) { - try { - // Próbujemy stworzyć widżet BarcodeWidget, aby sprawdzić poprawność kodu kreskowego - BarcodeWidget( - barcode: barcodeType, - data: data, - width: 200, - height: 100, - ); - widget.onAddBarcode(description, data, barcodeType); + if (barcodeType.isValid(data)) { + widget.onAddBarcode(description, data, barcodeType.barcode); _descriptionController.clear(); _dataController.clear(); - } catch (e) { + } else { _dataError = "Nieprawidłowy kod kreskowy"; } }