improve ui

This commit is contained in:
Marco 2024-06-01 12:41:58 +02:00
parent 7712373388
commit a5afda8de6
3 changed files with 122 additions and 40 deletions

View File

@ -13,38 +13,113 @@ class EnterFoodWidget extends StatefulWidget {
class _EnterFoodWidgetState extends State<EnterFoodWidget> { class _EnterFoodWidgetState extends State<EnterFoodWidget> {
String perFoodresult = "dings"; String perFoodresult = "dings";
TextEditingController nameController = TextEditingController();
TextEditingController massController = TextEditingController();
TextEditingController kcalPerMassController = TextEditingController();
@override @override
Widget build(BuildContext context) { 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( return Column(
children: [ children: [
Row( RowWidgetWithSpacers(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, nameWidget,
children: [ massWidget,
const Spacer(flex: 1), kcalPerMassWidget,
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), Padding(
const Spacer(flex: 1), padding: const EdgeInsets.all(8.0),
], child: ElevatedButton(
),
ElevatedButton(
child: const Text("Hinzufügen"), child: const Text("Hinzufügen"),
onPressed: () { onPressed: () {
var entry = double massAsNumber = 0.0;
FoodEntry(name: '', mass: 2.0, kcal: 100, kcalPerMass: 200); 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)); 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),
], ],
); );
} }

View File

@ -1,16 +1,23 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:kalodings/enter_food_widget.dart';
class FoodEntryWidget extends StatelessWidget { 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return const Row( return RowWidgetWithSpacers(
children: [ Text(name),
Text("Hier könnte ihre Werbung stehen"), Text(mass.toString()),
Text("Hier könnte ihre Werbung stehen"), Text(kcalPerMass.toString()),
Text("Hier könnte ihre Werbung stehen"),
],
); );
} }
} }

View File

@ -22,7 +22,10 @@ class _PerDayState extends State<PerDay> {
var newEntryWidgets = <FoodEntryWidget>[]; var newEntryWidgets = <FoodEntryWidget>[];
for (var entry in state.foodEntries) { for (var entry in state.foodEntries) {
newEntryWidgets.add(FoodEntryWidget()); newEntryWidgets.add(FoodEntryWidget(
name: entry.name,
mass: entry.mass,
kcalPerMass: entry.kcalPerMass));
} }
setState(() { setState(() {
entryList = newEntryWidgets; entryList = newEntryWidgets;
@ -31,11 +34,8 @@ class _PerDayState extends State<PerDay> {
child: ListView.builder( child: ListView.builder(
itemCount: entryList.length + 1, itemCount: entryList.length + 1,
itemBuilder: (BuildContext itemBuilderContext, int index) { itemBuilder: (BuildContext itemBuilderContext, int index) {
if (index + 1 == entryList.length + 1) if (index + 1 == entryList.length + 1) return EnterFoodWidget();
return EnterFoodWidget( return entryList[index];
perFoodresult: '',
);
return Text(index.toString());
}, },
), ),
); );