2023-07-30 13:09:41 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2023-07-30 14:40:45 +02:00
|
|
|
import 'package:flutter/services.dart';
|
2023-07-30 15:31:36 +02:00
|
|
|
import 'package:fooder/screens/based.dart';
|
|
|
|
import 'package:fooder/models/product.dart';
|
|
|
|
import 'package:fooder/models/entry.dart';
|
|
|
|
import 'package:fooder/widgets/product.dart';
|
|
|
|
import 'package:fooder/screens/add_product.dart';
|
2024-04-04 19:03:41 +02:00
|
|
|
import 'package:fooder/components/text.dart';
|
|
|
|
import 'package:fooder/components/floating_action_button.dart';
|
2024-03-25 19:46:03 +01:00
|
|
|
import 'package:simple_barcode_scanner/simple_barcode_scanner.dart';
|
2023-07-30 13:09:41 +02:00
|
|
|
|
|
|
|
class EditEntryScreen extends BasedScreen {
|
|
|
|
final Entry entry;
|
|
|
|
|
2023-08-28 14:45:32 +02:00
|
|
|
const EditEntryScreen(
|
|
|
|
{super.key, required super.apiClient, required this.entry});
|
2023-07-30 13:09:41 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<EditEntryScreen> createState() => _EditEntryScreen();
|
|
|
|
}
|
|
|
|
|
2024-04-04 19:03:41 +02:00
|
|
|
class _EditEntryScreen extends BasedState<EditEntryScreen> {
|
2023-07-30 13:09:41 +02:00
|
|
|
final gramsController = TextEditingController();
|
|
|
|
final productNameController = TextEditingController();
|
|
|
|
List<Product> products = [];
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
gramsController.dispose();
|
|
|
|
productNameController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2023-08-26 22:56:43 +02:00
|
|
|
void popMeDaddy() {
|
2023-07-30 13:09:41 +02:00
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-08-28 14:45:32 +02:00
|
|
|
void initState() {
|
2023-07-30 13:09:41 +02:00
|
|
|
super.initState();
|
|
|
|
setState(() {
|
|
|
|
gramsController.text = widget.entry.grams.toString();
|
|
|
|
productNameController.text = widget.entry.product.name;
|
|
|
|
products = [widget.entry.product];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _getProducts() async {
|
2023-08-28 14:45:32 +02:00
|
|
|
var productsMap =
|
|
|
|
await widget.apiClient.getProducts(productNameController.text);
|
2023-07-30 13:09:41 +02:00
|
|
|
setState(() {
|
2023-08-28 14:45:32 +02:00
|
|
|
products = (productsMap['products'] as List<dynamic>)
|
|
|
|
.map((e) => Product.fromJson(e as Map<String, dynamic>))
|
|
|
|
.toList();
|
2023-07-30 13:09:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-31 12:49:01 +02:00
|
|
|
Future<double?> _parseDouble(String text, String name) async {
|
|
|
|
try {
|
|
|
|
return double.parse(text.replaceAll(",", "."));
|
|
|
|
} catch (e) {
|
|
|
|
showError("$name must be a number");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-30 13:09:41 +02:00
|
|
|
Future<void> _saveEntry() async {
|
|
|
|
if (products.length != 1) {
|
|
|
|
showError("Pick product first");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-31 12:49:01 +02:00
|
|
|
var grams = await _parseDouble(gramsController.text, "Grams");
|
|
|
|
if (grams == null) {
|
2023-07-30 13:09:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await widget.apiClient.updateEntry(
|
|
|
|
widget.entry.id,
|
2023-07-31 12:49:01 +02:00
|
|
|
grams: grams,
|
2023-07-30 13:09:41 +02:00
|
|
|
productId: products[0].id,
|
|
|
|
mealId: widget.entry.mealId,
|
|
|
|
);
|
2023-08-26 22:56:43 +02:00
|
|
|
popMeDaddy();
|
2023-07-30 13:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _deleteEntry() async {
|
|
|
|
await widget.apiClient.deleteEntry(widget.entry.id);
|
2023-08-26 22:56:43 +02:00
|
|
|
popMeDaddy();
|
2023-07-30 13:09:41 +02:00
|
|
|
}
|
|
|
|
|
2024-03-25 19:46:03 +01:00
|
|
|
Future<void> _findProductByBarCode() async {
|
|
|
|
var res = await Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => const SimpleBarcodeScannerPage(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (res is String) {
|
|
|
|
try {
|
2024-03-30 14:07:10 +01:00
|
|
|
var productMap = await widget.apiClient.getProductByBarcode(res);
|
2024-03-25 19:46:03 +01:00
|
|
|
|
|
|
|
var product = Product.fromJson(productMap);
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
products = [product];
|
|
|
|
productNameController.text = product.name;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
showError("Product not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-30 13:09:41 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2024-04-04 19:03:41 +02:00
|
|
|
appBar: appBar(),
|
2023-07-30 13:09:41 +02:00
|
|
|
body: Center(
|
|
|
|
child: Container(
|
2023-08-28 14:45:32 +02:00
|
|
|
constraints: const BoxConstraints(maxWidth: 720),
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: ListView(children: <Widget>[
|
2024-04-04 19:03:41 +02:00
|
|
|
FTextInput(
|
|
|
|
labelText: 'Product name',
|
2023-10-27 13:28:24 +02:00
|
|
|
controller: productNameController,
|
|
|
|
onChanged: (_) => _getProducts(),
|
|
|
|
autofocus: true,
|
|
|
|
),
|
2024-04-04 19:03:41 +02:00
|
|
|
FTextInput(
|
|
|
|
labelText: 'Grams',
|
2023-08-28 14:45:32 +02:00
|
|
|
keyboardType:
|
|
|
|
const TextInputType.numberWithOptions(decimal: true),
|
2023-07-30 14:40:45 +02:00
|
|
|
inputFormatters: <TextInputFormatter>[
|
2023-08-28 14:45:32 +02:00
|
|
|
FilteringTextInputFormatter.allow(
|
|
|
|
RegExp(r'^(\d+)?[\.,]?\d{0,2}')),
|
2023-07-30 14:40:45 +02:00
|
|
|
],
|
2023-07-30 13:09:41 +02:00
|
|
|
controller: gramsController,
|
|
|
|
),
|
2023-07-30 15:05:20 +02:00
|
|
|
TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => AddProductScreen(
|
|
|
|
apiClient: widget.apiClient,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
).then((product) {
|
|
|
|
if (product == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setState(() {
|
|
|
|
products = [product];
|
|
|
|
productNameController.text = product.name;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
child: const Text("Don't see your product? Add it!"),
|
|
|
|
),
|
2023-07-30 13:09:41 +02:00
|
|
|
for (var product in products)
|
|
|
|
ListTile(
|
2023-08-28 14:45:32 +02:00
|
|
|
onTap: () {
|
|
|
|
setState(() {
|
|
|
|
products = [product];
|
|
|
|
productNameController.text = product.name;
|
2023-07-30 13:09:41 +02:00
|
|
|
});
|
2023-08-28 14:45:32 +02:00
|
|
|
},
|
|
|
|
title: ProductWidget(
|
|
|
|
product: product,
|
|
|
|
),
|
2023-07-30 13:09:41 +02:00
|
|
|
),
|
2023-08-28 14:45:32 +02:00
|
|
|
])),
|
2023-07-30 13:09:41 +02:00
|
|
|
),
|
|
|
|
floatingActionButton: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
children: <Widget>[
|
2024-04-04 19:03:41 +02:00
|
|
|
FActionButton(
|
2024-03-25 19:46:03 +01:00
|
|
|
onPressed: _findProductByBarCode,
|
2024-04-04 19:03:41 +02:00
|
|
|
icon: Icons.photo_camera,
|
2024-03-25 19:46:03 +01:00
|
|
|
),
|
2024-04-04 19:03:41 +02:00
|
|
|
const SizedBox(width: 10),
|
|
|
|
FActionButton(
|
2023-07-30 13:09:41 +02:00
|
|
|
onPressed: _deleteEntry,
|
2024-04-04 19:03:41 +02:00
|
|
|
tag: "fap1",
|
|
|
|
icon: Icons.delete,
|
2023-07-30 13:09:41 +02:00
|
|
|
),
|
2024-04-04 19:03:41 +02:00
|
|
|
const SizedBox(width: 10),
|
|
|
|
FActionButton(
|
2023-07-30 13:09:41 +02:00
|
|
|
onPressed: _saveEntry,
|
2024-04-04 19:03:41 +02:00
|
|
|
tag: "fap2",
|
|
|
|
icon: Icons.save,
|
2023-07-30 13:09:41 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|