From a5afda8de67f223f9f16d7a5e6f12ea7989d348c Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 1 Jun 2024 12:41:58 +0200 Subject: [PATCH] improve ui --- lib/enter_food_widget.dart | 129 +++++++++++++++++++++++++++++-------- lib/food_entry_widget.dart | 21 ++++-- lib/perday_widget.dart | 12 ++-- 3 files changed, 122 insertions(+), 40 deletions(-) diff --git a/lib/enter_food_widget.dart b/lib/enter_food_widget.dart index da826da..71a765d 100644 --- a/lib/enter_food_widget.dart +++ b/lib/enter_food_widget.dart @@ -13,38 +13,113 @@ class EnterFoodWidget extends StatefulWidget { 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: [ - Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - const Spacer(flex: 1), - const Expanded( - flex: 4, - child: - TextField(decoration: InputDecoration(hintText: "Name"))), - const Expanded( - flex: 4, - child: - TextField(decoration: InputDecoration(hintText: "Menge"))), - const Expanded( - flex: 4, - child: TextField( - decoration: InputDecoration(hintText: "kcal pro 100g")), - ), - Text(perFoodresult), - const Spacer(flex: 1), - ], + 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)); + }), ), - ElevatedButton( - child: const Text("Hinzufügen"), - onPressed: () { - var entry = - FoodEntry(name: '', mass: 2.0, kcal: 100, 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), ], ); } diff --git a/lib/food_entry_widget.dart b/lib/food_entry_widget.dart index 2c7df7f..b342e8a 100644 --- a/lib/food_entry_widget.dart +++ b/lib/food_entry_widget.dart @@ -1,16 +1,23 @@ import 'package:flutter/material.dart'; +import 'package:kalodings/enter_food_widget.dart'; class FoodEntryWidget extends StatelessWidget { - const FoodEntryWidget({super.key}); + final String name; + final double mass; + final double kcalPerMass; + + const FoodEntryWidget( + {super.key, + required this.name, + required this.mass, + required this.kcalPerMass}); @override Widget build(BuildContext context) { - return const Row( - children: [ - Text("Hier könnte ihre Werbung stehen"), - Text("Hier könnte ihre Werbung stehen"), - Text("Hier könnte ihre Werbung stehen"), - ], + return RowWidgetWithSpacers( + Text(name), + Text(mass.toString()), + Text(kcalPerMass.toString()), ); } } diff --git a/lib/perday_widget.dart b/lib/perday_widget.dart index 1c1873d..1e20c04 100644 --- a/lib/perday_widget.dart +++ b/lib/perday_widget.dart @@ -22,7 +22,10 @@ class _PerDayState extends State { var newEntryWidgets = []; for (var entry in state.foodEntries) { - newEntryWidgets.add(FoodEntryWidget()); + newEntryWidgets.add(FoodEntryWidget( + name: entry.name, + mass: entry.mass, + kcalPerMass: entry.kcalPerMass)); } setState(() { entryList = newEntryWidgets; @@ -31,11 +34,8 @@ class _PerDayState extends State { child: ListView.builder( itemCount: entryList.length + 1, itemBuilder: (BuildContext itemBuilderContext, int index) { - if (index + 1 == entryList.length + 1) - return EnterFoodWidget( - perFoodresult: '', - ); - return Text(index.toString()); + if (index + 1 == entryList.length + 1) return EnterFoodWidget(); + return entryList[index]; }, ), );