2024-09-04 20:47:32 +00:00
|
|
|
import 'package:calodiary/storage/storage.dart';
|
2024-05-29 22:58:26 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-09-06 11:48:56 +00:00
|
|
|
import 'package:calodiary/food_entry/food_entry_bloc.dart';
|
2024-06-11 17:32:25 +00:00
|
|
|
import 'package:calodiary/row_with_spacers_widget.dart';
|
2024-05-29 22:58:26 +00:00
|
|
|
|
|
|
|
class EnterFoodWidget extends StatefulWidget {
|
2024-06-09 17:06:10 +00:00
|
|
|
final Function(BuildContext context, FoodEntry entry) onAdd;
|
|
|
|
|
|
|
|
const EnterFoodWidget({super.key, required this.onAdd});
|
2024-05-29 22:58:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<EnterFoodWidget> createState() => _EnterFoodWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _EnterFoodWidgetState extends State<EnterFoodWidget> {
|
|
|
|
String perFoodresult = "dings";
|
2024-06-01 10:41:58 +00:00
|
|
|
TextEditingController nameController = TextEditingController();
|
|
|
|
TextEditingController massController = TextEditingController();
|
|
|
|
TextEditingController kcalPerMassController = TextEditingController();
|
2024-09-04 20:47:32 +00:00
|
|
|
Map<String, double> suggestions = {};
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
suggestions = FoodStorage.getInstance().getFoodEntryLookupDatabase;
|
|
|
|
super.initState();
|
|
|
|
}
|
2024-06-01 10:41:58 +00:00
|
|
|
|
2024-05-29 22:58:26 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-09-04 20:47:32 +00:00
|
|
|
var nameWidget = Autocomplete<String>(
|
|
|
|
optionsViewOpenDirection: OptionsViewOpenDirection.down,
|
|
|
|
fieldViewBuilder: (context, controller, focusNode, onSubmitted) {
|
|
|
|
nameController = controller;
|
|
|
|
return TextFormField(
|
2024-09-06 00:23:47 +00:00
|
|
|
controller: controller,
|
|
|
|
focusNode: focusNode,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
label: Text("Name"),
|
|
|
|
),
|
|
|
|
);
|
2024-09-04 20:47:32 +00:00
|
|
|
},
|
|
|
|
optionsBuilder: (TextEditingValue textEditingValue) {
|
|
|
|
if (textEditingValue.text == '') {
|
|
|
|
return const Iterable<String>.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return suggestions.keys.where(
|
|
|
|
(name) {
|
|
|
|
return name
|
|
|
|
.toLowerCase()
|
|
|
|
.contains(textEditingValue.text.toLowerCase());
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onSelected: (selectedFood) {
|
|
|
|
double kcalPerMassForSelectedFood = suggestions[selectedFood]!;
|
|
|
|
setState(() {
|
|
|
|
kcalPerMassController.text = kcalPerMassForSelectedFood.toString();
|
|
|
|
});
|
|
|
|
});
|
2024-06-01 10:41:58 +00:00
|
|
|
|
|
|
|
var massWidget = TextField(
|
2024-09-06 00:23:47 +00:00
|
|
|
textAlign: TextAlign.end,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
label: Align(alignment: Alignment.centerRight, child: Text("Menge")),
|
|
|
|
),
|
2024-06-01 10:41:58 +00:00
|
|
|
keyboardType: TextInputType.number,
|
|
|
|
controller: massController,
|
2024-09-06 00:23:47 +00:00
|
|
|
onSubmitted: (value) => onSubmitAction(),
|
2024-06-01 10:41:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
var kcalPerMassWidget = TextField(
|
2024-09-06 00:23:47 +00:00
|
|
|
textAlign: TextAlign.end,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
label:
|
|
|
|
Align(alignment: Alignment.centerRight, child: Text("kcal pro"))),
|
|
|
|
keyboardType: TextInputType.number,
|
|
|
|
controller: kcalPerMassController,
|
|
|
|
onSubmitted: (value) => onSubmitAction(),
|
|
|
|
);
|
2024-06-01 10:41:58 +00:00
|
|
|
|
2024-06-09 12:42:17 +00:00
|
|
|
var enterButton = ElevatedButton(
|
2024-06-10 15:35:21 +00:00
|
|
|
style: ElevatedButton.styleFrom(
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
),
|
2024-09-06 00:23:47 +00:00
|
|
|
onPressed: () => onSubmitAction(),
|
2024-06-09 12:42:17 +00:00
|
|
|
child: const Icon(Icons.add));
|
2024-06-01 10:41:58 +00:00
|
|
|
|
2024-06-10 15:35:21 +00:00
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.only(left: 8.0, right: 4.0),
|
|
|
|
child: RowWidget(
|
|
|
|
nameWidget,
|
|
|
|
massWidget,
|
|
|
|
kcalPerMassWidget,
|
2024-09-06 00:23:47 +00:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(left: 16.0),
|
|
|
|
child: enterButton,
|
|
|
|
),
|
2024-06-10 15:35:21 +00:00
|
|
|
),
|
2024-05-29 22:58:26 +00:00
|
|
|
);
|
|
|
|
}
|
2024-09-06 00:23:47 +00:00
|
|
|
|
|
|
|
void onSubmitAction() {
|
|
|
|
double massAsNumber = 0.0;
|
|
|
|
double kcalPerMassAsNumber = 0.0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
massAsNumber = double.parse(massController.text.replaceAll(",", "."));
|
|
|
|
} catch (e) {
|
|
|
|
var snackbar = const SnackBar(content: Text("Menge muss eine Zahl sein"));
|
|
|
|
ScaffoldMessenger.of(context).clearSnackBars();
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(snackbar);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
kcalPerMassAsNumber =
|
|
|
|
double.parse(kcalPerMassController.text.replaceAll(",", "."));
|
|
|
|
} catch (e) {
|
|
|
|
var snackbar =
|
|
|
|
const SnackBar(content: Text("'kcal pro 100g' muss eine Zahl sein"));
|
|
|
|
ScaffoldMessenger.of(context).clearSnackBars();
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(snackbar);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
massAsNumber = double.parse(massController.text.replaceAll(",", "."));
|
|
|
|
} catch (e) {
|
|
|
|
var snackbar = const SnackBar(content: Text("Menge muss eine Zahl sein"));
|
|
|
|
ScaffoldMessenger.of(context).clearSnackBars();
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(snackbar);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var entry = FoodEntry(
|
|
|
|
name: nameController.text,
|
|
|
|
mass: massAsNumber,
|
|
|
|
kcalPerMass: kcalPerMassAsNumber);
|
|
|
|
|
|
|
|
widget.onAdd(context, entry);
|
|
|
|
}
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|