2023-07-29 23:54:51 +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/diary.dart';
|
|
|
|
import 'package:fooder/models/meal.dart';
|
|
|
|
import 'package:fooder/widgets/product.dart';
|
|
|
|
import 'package:fooder/screens/add_product.dart';
|
2023-07-29 23:54:51 +02:00
|
|
|
|
|
|
|
class AddEntryScreen extends BasedScreen {
|
|
|
|
final Diary diary;
|
|
|
|
|
2023-08-28 14:45:32 +02:00
|
|
|
const AddEntryScreen(
|
|
|
|
{super.key, required super.apiClient, required this.diary});
|
2023-07-29 23:54:51 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<AddEntryScreen> createState() => _AddEntryScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AddEntryScreen extends State<AddEntryScreen> {
|
|
|
|
final gramsController = TextEditingController();
|
|
|
|
final productNameController = TextEditingController();
|
2023-07-30 14:40:45 +02:00
|
|
|
Meal? meal;
|
2023-07-29 23:54:51 +02:00
|
|
|
List<Product> products = [];
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
gramsController.dispose();
|
|
|
|
productNameController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2023-08-26 22:56:43 +02:00
|
|
|
void popMeDaddy() {
|
2023-07-29 23:54:51 +02:00
|
|
|
Navigator.pop(
|
|
|
|
context,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-08-28 14:45:32 +02:00
|
|
|
void initState() {
|
2023-07-29 23:54:51 +02:00
|
|
|
super.initState();
|
2023-07-30 14:40:45 +02:00
|
|
|
setState(() {
|
|
|
|
meal = widget.diary.meals[0];
|
|
|
|
});
|
2023-07-29 23:54:51 +02:00
|
|
|
_getProducts().then((value) => null);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _getProducts() async {
|
2023-08-28 14:45:32 +02:00
|
|
|
var productsMap =
|
|
|
|
await widget.apiClient.getProducts(productNameController.text);
|
2023-07-29 23:54:51 +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-29 23:54:51 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-28 14:45:32 +02:00
|
|
|
void showError(String message) {
|
2023-07-29 23:54:51 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(message, textAlign: TextAlign.center),
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.error,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-26 22:56:43 +02:00
|
|
|
Future<double?> _parseDouble(String text) async {
|
2023-07-31 12:49:01 +02:00
|
|
|
try {
|
|
|
|
return double.parse(text.replaceAll(",", "."));
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 22:56:43 +02:00
|
|
|
Future<void> _addEntry({bool silent = false}) async {
|
2023-07-29 23:54:51 +02:00
|
|
|
if (products.length != 1) {
|
2023-08-26 22:56:43 +02:00
|
|
|
if (!silent) {
|
|
|
|
showError("Pick one product");
|
|
|
|
}
|
2023-07-29 23:54:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-26 22:56:43 +02:00
|
|
|
var grams = await _parseDouble(gramsController.text);
|
2023-07-31 12:49:01 +02:00
|
|
|
if (grams == null) {
|
2023-08-26 22:56:43 +02:00
|
|
|
if (!silent) {
|
|
|
|
showError("Grams must be a number");
|
|
|
|
}
|
2023-07-29 23:54:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await widget.apiClient.addEntry(
|
2023-07-31 12:49:01 +02:00
|
|
|
grams: grams,
|
2023-07-29 23:54:51 +02:00
|
|
|
productId: products[0].id,
|
2023-07-30 19:12:51 +02:00
|
|
|
mealId: meal!.id,
|
2023-07-29 23:54:51 +02:00
|
|
|
);
|
2023-08-26 22:56:43 +02:00
|
|
|
popMeDaddy();
|
2023-07-29 23:54:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
2023-10-27 13:48:45 +02:00
|
|
|
title: Text("🅵🅾🅾🅳🅴🆁", style: logoStyle(context)),
|
2023-07-29 23:54:51 +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>[
|
2023-07-30 14:40:45 +02:00
|
|
|
DropdownButton<Meal>(
|
|
|
|
value: meal,
|
|
|
|
// Callback that sets the selected popup menu item.
|
|
|
|
onChanged: (Meal? meal) {
|
|
|
|
if (meal == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setState(() {
|
|
|
|
this.meal = meal;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
items: <DropdownMenuItem<Meal>>[
|
|
|
|
for (var meal in widget.diary.meals)
|
|
|
|
DropdownMenuItem<Meal>(
|
|
|
|
value: meal,
|
|
|
|
child: Text(meal.name),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2023-10-27 13:28:24 +02:00
|
|
|
TextFormField(
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Product name',
|
|
|
|
),
|
|
|
|
controller: productNameController,
|
|
|
|
onChanged: (_) => _getProducts(),
|
|
|
|
onFieldSubmitted: (_) => _addEntry(),
|
|
|
|
autofocus: true,
|
|
|
|
),
|
2023-07-29 23:54:51 +02:00
|
|
|
TextFormField(
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
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-29 23:54:51 +02:00
|
|
|
controller: gramsController,
|
2023-08-03 12:19:02 +02:00
|
|
|
onFieldSubmitted: (_) => _addEntry(),
|
2023-07-29 23:54:51 +02:00
|
|
|
),
|
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-29 23:54:51 +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
|
|
|
_addEntry(silent: true);
|
|
|
|
},
|
|
|
|
title: ProductWidget(
|
|
|
|
product: product,
|
|
|
|
),
|
2023-07-29 23:54:51 +02:00
|
|
|
),
|
2023-08-28 14:45:32 +02:00
|
|
|
])),
|
2023-07-29 23:54:51 +02:00
|
|
|
),
|
|
|
|
floatingActionButton: FloatingActionButton(
|
|
|
|
onPressed: _addEntry,
|
|
|
|
child: const Icon(Icons.add),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|