-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved price product selection
Deleted files: * `price_product_search_page.dart` New file: * `price_add_product_card.dart`: Card where the user can input a price product: type the barcode or scan. Impacted files: * `price_amount_card.dart`: removed the "no product" option; refactored Provider and Controllers * `price_amount_model.dart`: minor refactoring * `price_meta_product.dart`: removed the "no product" option; async load of data (local+server) * `price_model.dart`: minor refactoring * `product_price_add_page.dart`: refactored Provider; now using new widget `PriceAddProductCard` * `user_preferences_account.dart`: minor refactoring
- Loading branch information
1 parent
c50a10c
commit 2229069
Showing
8 changed files
with
335 additions
and
426 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
packages/smooth_app/lib/pages/prices/price_add_product_card.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:smooth_app/database/local_database.dart'; | ||
import 'package:smooth_app/generic_lib/buttons/smooth_large_button_with_icon.dart'; | ||
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart'; | ||
import 'package:smooth_app/generic_lib/widgets/smooth_card.dart'; | ||
import 'package:smooth_app/generic_lib/widgets/smooth_text_form_field.dart'; | ||
import 'package:smooth_app/pages/prices/price_amount_model.dart'; | ||
import 'package:smooth_app/pages/prices/price_meta_product.dart'; | ||
import 'package:smooth_app/pages/prices/price_model.dart'; | ||
import 'package:smooth_app/pages/prices/price_scan_page.dart'; | ||
|
||
/// Card where the user can input a price product: type the barcode or scan. | ||
class PriceAddProductCard extends StatelessWidget { | ||
const PriceAddProductCard(); | ||
|
||
static const TextInputType _textInputType = TextInputType.number; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final AppLocalizations appLocalizations = AppLocalizations.of(context); | ||
return SmoothCard( | ||
child: Column( | ||
children: <Widget>[ | ||
ListTile( | ||
title: Text( | ||
appLocalizations.prices_add_an_item, | ||
), | ||
), | ||
SmoothLargeButtonWithIcon( | ||
text: appLocalizations.barcode, | ||
icon: Icons.text_fields, | ||
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(), | ||
), | ||
), | ||
), | ||
); | ||
if (barcode == null) { | ||
return; | ||
} | ||
if (!context.mounted) { | ||
return; | ||
} | ||
await _addToList(barcode, context); | ||
}, | ||
), | ||
SmoothLargeButtonWithIcon( | ||
text: appLocalizations.prices_barcode_reader_action, | ||
icon: Icons.barcode_reader, | ||
onPressed: () async { | ||
final String? barcode = await Navigator.of(context).push<String>( | ||
MaterialPageRoute<String>( | ||
builder: (BuildContext context) => const PriceScanPage(), | ||
), | ||
); | ||
if (barcode == null) { | ||
return; | ||
} | ||
if (!context.mounted) { | ||
return; | ||
} | ||
await _addToList(barcode, context); | ||
}, | ||
), | ||
], | ||
), | ||
); | ||
} | ||
|
||
Future<void> _addToList( | ||
final String barcode, | ||
final BuildContext context, | ||
) async { | ||
final LocalDatabase localDatabase = context.read<LocalDatabase>(); | ||
final PriceModel priceModel = Provider.of<PriceModel>( | ||
context, | ||
listen: false, | ||
); | ||
priceModel.priceAmountModels.add( | ||
PriceAmountModel( | ||
product: PriceMetaProduct.unknown( | ||
barcode, | ||
localDatabase, | ||
priceModel, | ||
), | ||
), | ||
); | ||
priceModel.notifyListeners(); | ||
} | ||
|
||
bool _isValidBarcode(final String barcode) => barcode.length >= 8; | ||
|
||
// Probably there's a regexp for that, but at least it's readable code. | ||
String _getCleanBarcode(final String input) { | ||
const int ascii0 = 48; | ||
const int ascii9 = 48 + 10 - 1; | ||
|
||
final StringBuffer buffer = StringBuffer(); | ||
for (int i = 0; i < input.length; i++) { | ||
final int charCode = input.codeUnitAt(i); | ||
if (charCode >= ascii0 && charCode <= ascii9) { | ||
buffer.writeCharCode(charCode); | ||
} | ||
} | ||
return buffer.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.