diff --git a/lib/enter_food_widget.dart b/lib/enter_food_widget.dart index ea59a20..485b0db 100644 --- a/lib/enter_food_widget.dart +++ b/lib/enter_food_widget.dart @@ -32,9 +32,12 @@ class _EnterFoodWidgetState extends State { fieldViewBuilder: (context, controller, focusNode, onSubmitted) { nameController = controller; return TextFormField( - controller: controller, - focusNode: focusNode, - decoration: const InputDecoration(label: Text("Name"))); + controller: controller, + focusNode: focusNode, + decoration: const InputDecoration( + label: Text("Name"), + ), + ); }, optionsBuilder: (TextEditingValue textEditingValue) { if (textEditingValue.text == '') { @@ -57,63 +60,30 @@ class _EnterFoodWidgetState extends State { }); var massWidget = TextField( - decoration: const InputDecoration(label: Text("Menge")), + textAlign: TextAlign.end, + decoration: const InputDecoration( + label: Align(alignment: Alignment.centerRight, child: Text("Menge")), + ), keyboardType: TextInputType.number, controller: massController, + onSubmitted: (value) => onSubmitAction(), ); var kcalPerMassWidget = TextField( - decoration: const InputDecoration(label: Text("kcal pro")), - keyboardType: TextInputType.number, - controller: kcalPerMassController); + textAlign: TextAlign.end, + decoration: const InputDecoration( + label: + Align(alignment: Alignment.centerRight, child: Text("kcal pro"))), + keyboardType: TextInputType.number, + controller: kcalPerMassController, + onSubmitted: (value) => onSubmitAction(), + ); var enterButton = ElevatedButton( style: ElevatedButton.styleFrom( padding: EdgeInsets.zero, ), - 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, - kcalPerMass: kcalPerMassAsNumber); - - widget.onAdd(context, entry); - }, + onPressed: () => onSubmitAction(), child: const Icon(Icons.add)); return Padding( @@ -122,9 +92,51 @@ class _EnterFoodWidgetState extends State { nameWidget, massWidget, kcalPerMassWidget, - null, - enterButton, + Padding( + padding: const EdgeInsets.only(left: 16.0), + child: enterButton, + ), ), ); } + + void onSubmitAction() { + 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, + kcalPerMass: kcalPerMassAsNumber); + + widget.onAdd(context, entry); + } } diff --git a/lib/food_entry_widget.dart b/lib/food_entry_widget.dart index cd6d1b0..a31c695 100644 --- a/lib/food_entry_widget.dart +++ b/lib/food_entry_widget.dart @@ -2,37 +2,93 @@ import 'package:flutter/material.dart'; import 'package:calodiary/food_entry_bloc.dart'; import 'package:calodiary/row_with_spacers_widget.dart'; -class FoodEntryWidget extends StatelessWidget { +class FoodEntryWidget extends StatefulWidget { final FoodEntry entry; final Function(BuildContext context, String id) onDelete; const FoodEntryWidget( {super.key, required this.entry, required this.onDelete}); + @override + State createState() => _FoodEntryWidgetState(); +} + +class _FoodEntryWidgetState extends State { + late bool showCancelAndDelete; + @override + void initState() { + showCancelAndDelete = false; + super.initState(); + } + @override Widget build(BuildContext context) { - return Dismissible( - key: ValueKey(entry.id), - onDismissed: (direction) { - onDelete(context, entry.id); + return GestureDetector( + onTap: () { + setState(() { + showCancelAndDelete = !showCancelAndDelete; + }); }, - child: Card( - elevation: 5.0, - child: Padding( - padding: const EdgeInsets.only(left: 4.0), - child: RowWidget( - Text(entry.name), - Text(entry.mass.ceil().toString()), - Text(entry.kcalPerMass.ceil().toString()), - Text((entry.mass * entry.kcalPerMass / 100).ceil().toString()), - IconButton( - style: IconButton.styleFrom(padding: EdgeInsets.zero), - onPressed: () { - onDelete(context, entry.id); - }, - icon: const Icon(Icons.delete_forever_rounded)), + child: Stack( + children: [ + Positioned.fill( + child: Stack(children: [ + Positioned.fill( + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 8.0), + child: RowWidget( + Text(widget.entry.name), + Text(widget.entry.mass.ceil().toString(), + textAlign: TextAlign.end), + Text(widget.entry.kcalPerMass.ceil().toString(), + textAlign: TextAlign.end), + Opacity( + opacity: showCancelAndDelete ? 0.0 : 1.0, + child: Text( + (widget.entry.mass * widget.entry.kcalPerMass / 100) + .ceil() + .toString(), + textAlign: TextAlign.end), + ), + ), + ), + ), + Opacity( + opacity: showCancelAndDelete ? 0.66 : 0.0, + child: Container( + color: Theme.of(context).colorScheme.secondary)), + ]), ), - ), + Opacity( + opacity: showCancelAndDelete ? 1.0 : 0.0, + child: Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + SizedBox( + child: IconButton( + padding: EdgeInsets.all(0.0), + icon: const Icon(Icons.cancel), + onPressed: showCancelAndDelete + ? () => setState(() { + showCancelAndDelete = false; + }) + : null, + ), + ), + SizedBox( + child: IconButton( + padding: EdgeInsets.all(0.0), + iconSize: 24, + icon: const Icon(Icons.delete), + color: Colors.redAccent, + onPressed: showCancelAndDelete + ? () => widget.onDelete(context, widget.entry.id) + : null), + ), + ], + ), + ), + ], ), ); } diff --git a/lib/main.dart b/lib/main.dart index 32d8740..07e4574 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -68,6 +68,7 @@ class MainApp extends StatelessWidget { Locale('de'), ], theme: ThemeData( + dividerTheme: const DividerThemeData(space: 2), colorScheme: ColorScheme.fromSeed( seedColor: Colors.lightBlue, brightness: newBrightness, diff --git a/lib/perdate_widget.dart b/lib/perdate_widget.dart index 3ec8c36..16e81f0 100644 --- a/lib/perdate_widget.dart +++ b/lib/perdate_widget.dart @@ -94,6 +94,7 @@ class _PerDateWidgetState extends State { return Column( children: [ FoodEntryWidget( + key: ValueKey(state.foodEntries[entryIndex].id), entry: state.foodEntries[entryIndex], onDelete: (callbackContext, id) { callbackContext @@ -103,8 +104,7 @@ class _PerDateWidgetState extends State { )); }, ), - if (listIndex != state.foodEntries.length - 1) - const Divider(), + const Divider(), ], ); }, diff --git a/lib/row_with_spacers_widget.dart b/lib/row_with_spacers_widget.dart index 5a529ac..b9654cb 100644 --- a/lib/row_with_spacers_widget.dart +++ b/lib/row_with_spacers_widget.dart @@ -1,31 +1,22 @@ import 'package:flutter/material.dart'; -class RowWidget extends StatefulWidget { +class RowWidget extends StatelessWidget { final Widget? widget1; final Widget? widget2; final Widget? widget3; final Widget? widget4; - final Widget? widget5; - const RowWidget( - this.widget1, this.widget2, this.widget3, this.widget4, this.widget5, + const RowWidget(this.widget1, this.widget2, this.widget3, this.widget4, {super.key}); - @override - State createState() => _RowWidgetState(); -} - -class _RowWidgetState extends State { @override Widget build(BuildContext context) { return Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Expanded(flex: 10, child: widget.widget1 ?? Container()), - Expanded(flex: 6, child: widget.widget2 ?? Container()), - Expanded(flex: 6, child: widget.widget3 ?? Container()), - Expanded(flex: 8, child: widget.widget4 ?? Container()), - Expanded(flex: 3, child: widget.widget5 ?? Container()), + Expanded(flex: 10, child: widget1 ?? Container()), + Expanded(flex: 6, child: widget2 ?? Container()), + Expanded(flex: 6, child: widget3 ?? Container()), + Expanded(flex: 6, child: widget4 ?? Container()), ], ); } diff --git a/lib/sum_widget.dart b/lib/sum_widget.dart index c800576..6b4088a 100644 --- a/lib/sum_widget.dart +++ b/lib/sum_widget.dart @@ -33,7 +33,6 @@ class _SumWidgetState extends State { null, null, null, - null, ); }, );