calorimeter/lib/enter_food_widget.dart

99 lines
3.1 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:calodiary/food_entry_bloc.dart';
import 'package:calodiary/row_with_spacers_widget.dart';
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});
@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();
@override
Widget build(BuildContext context) {
2024-06-01 10:41:58 +00:00
var nameWidget = TextField(
decoration: const InputDecoration(hintText: "Name"),
controller: nameController,
);
var massWidget = TextField(
decoration: const InputDecoration(hintText: "Menge"),
keyboardType: TextInputType.number,
controller: massController,
);
var kcalPerMassWidget = TextField(
decoration: const InputDecoration(hintText: "kcal pro 100g"),
2024-06-09 12:42:17 +00:00
keyboardType: TextInputType.number,
2024-06-01 10:41:58 +00:00
controller: kcalPerMassController);
2024-06-09 12:42:17 +00:00
var enterButton = ElevatedButton(
style: ElevatedButton.styleFrom(
padding: EdgeInsets.zero,
),
2024-06-09 12:42:17 +00:00
onPressed: () {
double massAsNumber = 0.0;
double kcalPerMassAsNumber = 0.0;
2024-06-01 10:41:58 +00:00
2024-06-09 12:42:17 +00:00
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;
}
2024-06-01 10:41:58 +00:00
2024-06-09 12:42:17 +00:00
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;
}
2024-06-01 10:41:58 +00:00
2024-06-09 12:42:17 +00:00
var entry = FoodEntry(
name: nameController.text,
mass: massAsNumber,
kcalPerMass: kcalPerMassAsNumber);
2024-06-01 10:41:58 +00:00
2024-06-09 17:06:10 +00:00
widget.onAdd(context, entry);
2024-06-09 12:42:17 +00:00
},
child: const Icon(Icons.add));
2024-06-01 10:41:58 +00:00
return Padding(
padding: const EdgeInsets.only(left: 8.0, right: 4.0),
child: RowWidget(
nameWidget,
massWidget,
kcalPerMassWidget,
null,
enterButton,
),
);
}
}