2024-06-09 12:42:17 +00:00
|
|
|
import 'dart:developer';
|
|
|
|
|
2024-05-29 22:58:26 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:kalodings/food_entry_bloc.dart';
|
2024-06-09 12:42:17 +00:00
|
|
|
import 'package:kalodings/row_with_spacers_widget.dart';
|
2024-05-29 22:58:26 +00:00
|
|
|
|
|
|
|
class EnterFoodWidget extends StatefulWidget {
|
|
|
|
const EnterFoodWidget({
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
|
|
|
@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-05-29 22:58:26 +00:00
|
|
|
@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(
|
|
|
|
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 12:42:17 +00:00
|
|
|
log('Entry id: ${entry.id}');
|
2024-06-01 10:41:58 +00:00
|
|
|
|
2024-06-09 12:42:17 +00:00
|
|
|
context.read<FoodEntryBloc>().add(FoodEntryEvent(entry: entry));
|
|
|
|
},
|
|
|
|
child: const Icon(Icons.add));
|
2024-06-01 10:41:58 +00:00
|
|
|
|
2024-06-09 12:42:17 +00:00
|
|
|
return Column(
|
2024-06-01 10:41:58 +00:00
|
|
|
children: [
|
2024-06-09 12:42:17 +00:00
|
|
|
RowWidgetWithSpacers(
|
|
|
|
nameWidget,
|
|
|
|
massWidget,
|
|
|
|
kcalPerMassWidget,
|
|
|
|
null,
|
|
|
|
enterButton,
|
2024-05-29 22:58:26 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|