Skip to content

Commit

Permalink
fixes review/13
Browse files Browse the repository at this point in the history
  • Loading branch information
D3bi7 committed Jul 15, 2024
1 parent ef6e8c3 commit 40c3141
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/barcode_item_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ class BarcodeItemWidget extends StatelessWidget {
);
}
}

9 changes: 9 additions & 0 deletions lib/barcode_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
15 changes: 4 additions & 11 deletions lib/barcodes_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,18 @@ class BarcodesPanelState extends State<BarcodesPanel> {
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";
}
}
Expand Down

0 comments on commit 40c3141

Please sign in to comment.