calorimeter/lib/enter_food_widget.dart
2024-06-01 12:41:58 +02:00

127 lines
4.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:kalodings/food_entry_bloc.dart';
class EnterFoodWidget extends StatefulWidget {
const EnterFoodWidget({
super.key,
});
@override
State<EnterFoodWidget> createState() => _EnterFoodWidgetState();
}
class _EnterFoodWidgetState extends State<EnterFoodWidget> {
String perFoodresult = "dings";
TextEditingController nameController = TextEditingController();
TextEditingController massController = TextEditingController();
TextEditingController kcalPerMassController = TextEditingController();
@override
Widget build(BuildContext context) {
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"),
controller: kcalPerMassController);
return Column(
children: [
RowWidgetWithSpacers(
nameWidget,
massWidget,
kcalPerMassWidget,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
child: const Text("Hinzufügen"),
onPressed: () {
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,
kcal: kcalPerMassAsNumber,
kcalPerMass: 200);
context.read<FoodEntryBloc>().add(FoodEntryEvent(entry: entry));
}),
),
],
);
}
}
class RowWidgetWithSpacers extends StatefulWidget {
final Widget widget1;
final Widget widget2;
final Widget widget3;
const RowWidgetWithSpacers(this.widget1, this.widget2, this.widget3,
{super.key});
@override
State<RowWidgetWithSpacers> createState() => _RowWidgetWithSpacersState();
}
class _RowWidgetWithSpacersState extends State<RowWidgetWithSpacers> {
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Spacer(flex: 1),
Expanded(flex: 4, child: widget.widget1),
Expanded(
flex: 4,
child: widget.widget2,
),
Expanded(flex: 4, child: widget.widget3),
const Spacer(flex: 1),
],
);
}
}