Skip to content

Commit

Permalink
fix: 5630 - changed button order for prices "new product" (openfoodfa…
Browse files Browse the repository at this point in the history
…cts#5633)

Impacted file:
* `price_add_product_card.dart`: minor refactoring
  • Loading branch information
monsieurtanuki authored Sep 27, 2024
1 parent a656986 commit 72056fc
Showing 1 changed file with 52 additions and 47 deletions.
99 changes: 52 additions & 47 deletions packages/smooth_app/lib/pages/prices/price_add_product_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,71 +36,34 @@ class _PriceAddProductCardState extends State<PriceAddProductCard> {
),
),
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<String>(
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<String>(
MaterialPageRoute<String>(
builder: (BuildContext context) => PriceScanPage(
latestScannedBarcode: _latestScannedBarcode,
),
),
);
if (barcode == null) {
return;
}
_latestScannedBarcode = barcode;
if (!context.mounted) {
return;
}
await _addToList(barcode, context);
},
),
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<String>(
MaterialPageRoute<String>(
builder: (BuildContext context) => PriceScanPage(
latestScannedBarcode: _latestScannedBarcode,
),
),
);
final String? barcode = await _textInput(context);
if (barcode == null) {
return;
}
_latestScannedBarcode = barcode;
if (!context.mounted) {
return;
}
Expand Down Expand Up @@ -149,6 +112,48 @@ class _PriceAddProductCardState extends State<PriceAddProductCard> {
priceModel.notifyListeners();
}

Future<String?> _textInput(final BuildContext context) async {
final TextEditingController controller = TextEditingController();
final AppLocalizations appLocalizations = AppLocalizations.of(context);
return showDialog<String>(
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.
Expand Down

0 comments on commit 72056fc

Please sign in to comment.