diff --git a/packages/smooth_app/lib/pages/prices/price_add_product_card.dart b/packages/smooth_app/lib/pages/prices/price_add_product_card.dart index 34f7e970238..fd1930d32d4 100644 --- a/packages/smooth_app/lib/pages/prices/price_add_product_card.dart +++ b/packages/smooth_app/lib/pages/prices/price_add_product_card.dart @@ -36,50 +36,20 @@ class _PriceAddProductCardState extends State { ), ), SmoothLargeButtonWithIcon( - text: appLocalizations.prices_barcode_enter, - icon: Icons.text_fields, + text: appLocalizations.prices_barcode_reader_action, + icon: Icons.barcode_reader, onPressed: () async { - final TextEditingController controller = TextEditingController(); - final String? barcode = await showDialog( - context: context, - builder: (final BuildContext context) => StatefulBuilder( - builder: ( - final BuildContext context, - void Function(VoidCallback fn) setState, - ) => - SmoothAlertDialog( - title: appLocalizations.prices_add_an_item, - body: SmoothTextFormField( - autofocus: true, - type: TextFieldTypes.PLAIN_TEXT, - controller: controller, - hintText: appLocalizations.barcode, - textInputType: _textInputType, - onChanged: (_) { - final String barcode = controller.text; - final String cleanBarcode = _getCleanBarcode(barcode); - setState(() => controller.text = cleanBarcode); - }, - onFieldSubmitted: (_) => !_isValidBarcode(controller.text) - ? null - : Navigator.of(context).pop(controller.text), - ), - positiveAction: SmoothActionButton( - text: appLocalizations.validate, - onPressed: !_isValidBarcode(controller.text) - ? null - : () => Navigator.of(context).pop(controller.text), - ), - negativeAction: SmoothActionButton( - text: appLocalizations.cancel, - onPressed: () => Navigator.of(context).pop(), - ), + final String? barcode = await Navigator.of(context).push( + MaterialPageRoute( + builder: (BuildContext context) => PriceScanPage( + latestScannedBarcode: _latestScannedBarcode, ), ), ); if (barcode == null) { return; } + _latestScannedBarcode = barcode; if (!context.mounted) { return; } @@ -87,20 +57,13 @@ class _PriceAddProductCardState extends State { }, ), SmoothLargeButtonWithIcon( - text: appLocalizations.prices_barcode_reader_action, - icon: Icons.barcode_reader, + text: appLocalizations.prices_barcode_enter, + icon: Icons.text_fields, onPressed: () async { - final String? barcode = await Navigator.of(context).push( - MaterialPageRoute( - builder: (BuildContext context) => PriceScanPage( - latestScannedBarcode: _latestScannedBarcode, - ), - ), - ); + final String? barcode = await _textInput(context); if (barcode == null) { return; } - _latestScannedBarcode = barcode; if (!context.mounted) { return; } @@ -149,6 +112,48 @@ class _PriceAddProductCardState extends State { priceModel.notifyListeners(); } + Future _textInput(final BuildContext context) async { + final TextEditingController controller = TextEditingController(); + final AppLocalizations appLocalizations = AppLocalizations.of(context); + return showDialog( + context: context, + builder: (final BuildContext context) => StatefulBuilder( + builder: ( + final BuildContext context, + void Function(VoidCallback fn) setState, + ) => + SmoothAlertDialog( + title: appLocalizations.prices_add_an_item, + body: SmoothTextFormField( + autofocus: true, + type: TextFieldTypes.PLAIN_TEXT, + controller: controller, + hintText: appLocalizations.barcode, + textInputType: _textInputType, + onChanged: (_) { + final String barcode = controller.text; + final String cleanBarcode = _getCleanBarcode(barcode); + setState(() => controller.text = cleanBarcode); + }, + onFieldSubmitted: (_) => !_isValidBarcode(controller.text) + ? null + : Navigator.of(context).pop(controller.text), + ), + positiveAction: SmoothActionButton( + text: appLocalizations.validate, + onPressed: !_isValidBarcode(controller.text) + ? null + : () => Navigator.of(context).pop(controller.text), + ), + negativeAction: SmoothActionButton( + text: appLocalizations.cancel, + onPressed: () => Navigator.of(context).pop(), + ), + ), + ), + ); + } + bool _isValidBarcode(final String barcode) => barcode.length >= 8; // Probably there's a regexp for that, but at least it's readable code.