diff --git a/lib/barcode_type.dart b/lib/barcode_type.dart new file mode 100644 index 0000000..65f6e45 --- /dev/null +++ b/lib/barcode_type.dart @@ -0,0 +1,26 @@ +import 'package:barcode_widget/barcode_widget.dart'; + +enum BarcodeType { + ean13, + ean8, +} + +extension BarcodeTypeExtension on BarcodeType { + String get name { + switch (this) { + case BarcodeType.ean13: + return 'EAN13'; + case BarcodeType.ean8: + return 'EAN8'; + } + } + + Barcode get barcode { + switch (this) { + case BarcodeType.ean13: + return Barcode.ean13(); + case BarcodeType.ean8: + return Barcode.ean8(); + } + } +} diff --git a/lib/barcodes_panel.dart b/lib/barcodes_panel.dart index 4fe47ab..6901062 100644 --- a/lib/barcodes_panel.dart +++ b/lib/barcodes_panel.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:barcode_widget/barcode_widget.dart'; +import 'barcode_type.dart' as custom; class BarcodesPanel extends StatefulWidget { const BarcodesPanel({ @@ -16,7 +17,7 @@ class BarcodesPanel extends StatefulWidget { class BarcodesPanelState extends State { final TextEditingController _descriptionController = TextEditingController(); final TextEditingController _dataController = TextEditingController(); - String _selectedBarcodeType = 'EAN13'; + custom.BarcodeType _selectedBarcodeType = custom.BarcodeType.ean13; @override void dispose() { @@ -28,13 +29,7 @@ class BarcodesPanelState extends State { void _handleAddBarcode() { final description = _descriptionController.text; final data = _dataController.text; - - Barcode barcodeType; - if (_selectedBarcodeType == 'EAN13') { - barcodeType = Barcode.ean13(); - } else { - barcodeType = Barcode.ean8(); - } + final barcodeType = _selectedBarcodeType.barcode; if (description.isNotEmpty && data.isNotEmpty) { widget.onAddBarcode(description, data, barcodeType); @@ -56,22 +51,18 @@ class BarcodesPanelState extends State { Widget _dropdownField() { return Expanded( - child: DropdownButtonFormField( + child: DropdownButtonFormField( value: _selectedBarcodeType, decoration: const InputDecoration( labelText: "Typ kodu", ), - items: const [ - DropdownMenuItem( - value: 'EAN13', - child: Text("EAN13"), - ), - DropdownMenuItem( - value: 'EAN8', - child: Text("EAN8"), - ), - ], - onChanged: (String? newValue) { + items: custom.BarcodeType.values.map((custom.BarcodeType type) { + return DropdownMenuItem( + value: type, + child: Text(type.name), + ); + }).toList(), + onChanged: (custom.BarcodeType? newValue) { setState(() { if (newValue != null) { _selectedBarcodeType = newValue;