From 985ed670e474487754e59b4d1e82048437a5c301 Mon Sep 17 00:00:00 2001 From: doman Date: Fri, 27 Oct 2023 20:06:41 +0200 Subject: [PATCH] [meal] view --- lib/screens/meal.dart | 174 +++++++++++++++++++++++++++++++++++++++++ lib/widgets/diary.dart | 43 +++++----- lib/widgets/meal.dart | 89 ++++----------------- 3 files changed, 211 insertions(+), 95 deletions(-) create mode 100644 lib/screens/meal.dart diff --git a/lib/screens/meal.dart b/lib/screens/meal.dart new file mode 100644 index 0000000..5c1e213 --- /dev/null +++ b/lib/screens/meal.dart @@ -0,0 +1,174 @@ +import 'package:flutter/material.dart'; +import 'package:fooder/screens/based.dart'; +import 'package:fooder/models/meal.dart'; +import 'package:fooder/widgets/macro.dart'; + +class MealScreen extends BasedScreen { + final Meal meal; + + const MealScreen({super.key, required super.apiClient, required this.meal}); + + @override + State createState() => _AddMealScreen(); +} + +class _AddMealScreen extends State { + Future saveMeal(context) async { + TextEditingController textFieldController = TextEditingController(); + textFieldController.text = widget.meal.name; + + showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: const Text('Save Meal'), + content: TextField( + controller: textFieldController, + decoration: const InputDecoration(hintText: "Meal template name"), + ), + actions: [ + IconButton( + icon: const Icon(Icons.cancel), + onPressed: () { + Navigator.pop(context); + }, + ), + IconButton( + icon: const Icon(Icons.save), + onPressed: () { + widget.apiClient + .saveMeal(widget.meal, textFieldController.text); + Navigator.pop(context); + }, + ), + ], + ); + }, + ); + } + + Future _deleteMeal(Meal meal) async { + await widget.apiClient.deleteMeal(meal.id); + } + + Future deleteMeal(context) async { + showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: const Text('Confirm deletion of the meal'), + actions: [ + IconButton( + icon: const Icon(Icons.cancel), + onPressed: () { + Navigator.pop(context); + }, + ), + IconButton( + icon: const Icon(Icons.delete), + onPressed: () { + _deleteMeal(widget.meal); + Navigator.pop(context); + }, + ), + ], + ); + }, + ); + } + + Widget buildButton(Icon icon, String text, Function() onPressed) { + return Card( + child: ListTile( + onTap: onPressed, + title: Container( + padding: const EdgeInsets.all(8), + child: Row( + children: [ + IconButton( + icon: icon, + onPressed: onPressed, + ), + const Spacer(), + Text(text), + ], + ), + ), + ), + ); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + backgroundColor: Theme.of(context).colorScheme.inversePrimary, + title: Text("πŸ…΅πŸ…ΎπŸ…ΎπŸ…³πŸ…΄πŸ†", style: logoStyle(context)), + ), + body: Center( + child: Container( + constraints: const BoxConstraints(maxWidth: 720), + padding: const EdgeInsets.all(10), + child: CustomScrollView(slivers: [ + SliverAppBar( + title: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + widget.meal.name, + style: Theme.of(context) + .textTheme + .headlineLarge! + .copyWith( + color: Theme.of(context).colorScheme.onSecondary, + fontWeight: FontWeight.bold, + ), + ), + const Spacer(), + Text( + "${widget.meal.calories.toStringAsFixed(1)} kcal", + style: Theme.of(context) + .textTheme + .headlineLarge! + .copyWith( + color: Theme.of(context).colorScheme.onSecondary, + fontWeight: FontWeight.bold, + ), + ), + ]), + expandedHeight: 150, + backgroundColor: Theme.of(context).colorScheme.secondary, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(8), + ), + floating: true, + flexibleSpace: FlexibleSpaceBar( + title: MacroWidget( + protein: widget.meal.protein, + carb: widget.meal.carb, + fat: widget.meal.fat, + style: Theme.of(context).textTheme.bodyMedium!.copyWith( + color: Theme.of(context).colorScheme.onSecondary, + fontWeight: FontWeight.bold, + ), + ), + )), + SliverList( + delegate: SliverChildListDelegate([ + buildButton( + const Icon(Icons.save), + "Save as preset", + () => saveMeal(context), + ), + buildButton( + const Icon(Icons.delete), + "Delete", + () => deleteMeal(context), + ), + ])) + ]), + ), + ), + ); + } +} diff --git a/lib/widgets/diary.dart b/lib/widgets/diary.dart index d773780..a182c4f 100644 --- a/lib/widgets/diary.dart +++ b/lib/widgets/diary.dart @@ -54,30 +54,29 @@ class DiaryWidget extends StatelessWidget { color: Theme.of(context).colorScheme.onSecondary, fontWeight: FontWeight.bold, ), - child: IconButton( - onPressed: () { - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => AddMealScreen( - apiClient: apiClient, - diary: diary, - ), - ), - ).then((_) { - refreshParent(); - }); - }, - icon: const Icon(Icons.add), - style: ButtonStyle( - backgroundColor: MaterialStateProperty.all( - Theme.of(context).colorScheme.secondary), - foregroundColor: MaterialStateProperty.all( - Theme.of(context).colorScheme.onSecondary), - ), - ), ), )), + SliverToBoxAdapter( + child: SizedBox( + height: 30, + child: TextButton( + onPressed: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => AddMealScreen( + apiClient: apiClient, + diary: diary, + ), + ), + ).then((_) { + refreshParent(); + }); + }, + child: const Text("Add Meal"), + ), + ), + ), SliverList( delegate: SliverChildListDelegate( [ diff --git a/lib/widgets/meal.dart b/lib/widgets/meal.dart index 7d02bf9..43a5a3f 100644 --- a/lib/widgets/meal.dart +++ b/lib/widgets/meal.dart @@ -3,6 +3,7 @@ import 'package:fooder/models/meal.dart'; import 'package:fooder/widgets/entry.dart'; import 'package:fooder/widgets/macro.dart'; import 'package:fooder/screens/edit_entry.dart'; +import 'package:fooder/screens/meal.dart'; import 'package:fooder/client.dart'; import 'dart:core'; @@ -17,73 +18,23 @@ class MealWidget extends StatelessWidget { required this.apiClient, required this.refreshParent}); - Future saveMeal(context) async { - TextEditingController textFieldController = TextEditingController(); - textFieldController.text = meal.name; - - showDialog( - context: context, - builder: (context) { - return AlertDialog( - title: const Text('Save Meal'), - content: TextField( - controller: textFieldController, - decoration: const InputDecoration(hintText: "Meal template name"), - ), - actions: [ - IconButton( - icon: const Icon(Icons.cancel), - onPressed: () { - Navigator.pop(context); - }, - ), - IconButton( - icon: const Icon(Icons.save), - onPressed: () { - apiClient.saveMeal(meal, textFieldController.text); - Navigator.pop(context); - }, - ), - ], - ); - }, - ); - } - - Future _deleteMeal(Meal meal) async { - await apiClient.deleteMeal(meal.id); - refreshParent(); - } - - Future deleteMeal(context) async { - showDialog( - context: context, - builder: (context) { - return AlertDialog( - title: const Text('Confirm deletion of the meal'), - actions: [ - IconButton( - icon: const Icon(Icons.cancel), - onPressed: () { - Navigator.pop(context); - }, - ), - IconButton( - icon: const Icon(Icons.delete), - onPressed: () { - _deleteMeal(meal); - Navigator.pop(context); - }, - ), - ], - ); - }, - ); - } - @override Widget build(BuildContext context) { return Card( + child: GestureDetector( + onLongPress: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => MealScreen( + apiClient: apiClient, + meal: meal, + ), + ), + ).then((_) { + refreshParent(); + }); + }, child: Padding( padding: const EdgeInsets.only( top: 36.0, left: 6.0, right: 6.0, bottom: 6.0), @@ -100,14 +51,6 @@ class MealWidget extends StatelessWidget { ), ), ), - IconButton( - icon: const Icon(Icons.delete), - onPressed: () {deleteMeal(context);}, - ), - IconButton( - icon: const Icon(Icons.save), - onPressed: () {saveMeal(context);}, - ), Text("${meal.calories.toStringAsFixed(1)} kcal"), ], ), @@ -144,6 +87,6 @@ class MealWidget extends StatelessWidget { ], ), ), - ); + )); } }