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 createState() => _EnterFoodWidgetState(); } class _EnterFoodWidgetState extends State { 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().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 createState() => _RowWidgetWithSpacersState(); } class _RowWidgetWithSpacersState extends State { @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), ], ); } }