From cd6dc9a1b39d71f980524deb7663f3b317d2e855 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Wed, 23 Dec 2020 10:48:53 +0100 Subject: [PATCH] Extract widget --- lib/widgets/nutrition/forms.dart | 22 ++-- lib/widgets/nutrition/meal.dart | 177 ++++++++++++++++--------------- 2 files changed, 100 insertions(+), 99 deletions(-) diff --git a/lib/widgets/nutrition/forms.dart b/lib/widgets/nutrition/forms.dart index 9a29b6b7d..4e1be1c52 100644 --- a/lib/widgets/nutrition/forms.dart +++ b/lib/widgets/nutrition/forms.dart @@ -19,7 +19,9 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:wger/helpers/json.dart'; +import 'package:wger/helpers/ui.dart'; import 'package:wger/locale/locales.dart'; +import 'package:wger/models/http_exception.dart'; import 'package:wger/models/nutrition/meal.dart'; import 'package:wger/models/nutrition/nutritional_plan.dart'; import 'package:wger/providers/nutritional_plans.dart'; @@ -78,24 +80,12 @@ class MealForm extends StatelessWidget { try { Provider.of(context, listen: false).addMeal(meal, _plan); - Navigator.of(context).pop(); + } on WgerHttpException catch (error) { + showHttpExceptionErrorDialog(error, context); } catch (error) { - await showDialog( - context: context, - builder: (ctx) => AlertDialog( - title: Text('An error occurred!'), - content: Text('Something went wrong.'), - actions: [ - TextButton( - child: Text('Okay'), - onPressed: () { - Navigator.of(ctx).pop(); - }, - ) - ], - ), - ); + showErrorDialog(error, context); } + Navigator.of(context).pop(); }, ), ], diff --git a/lib/widgets/nutrition/meal.dart b/lib/widgets/nutrition/meal.dart index 56bf29868..c5d1636d1 100644 --- a/lib/widgets/nutrition/meal.dart +++ b/lib/widgets/nutrition/meal.dart @@ -20,6 +20,26 @@ import 'package:flutter/material.dart'; import 'package:wger/models/nutrition/meal.dart'; import 'package:wger/models/nutrition/meal_item.dart'; +class MealWidget extends StatelessWidget { + final Meal _meal; + + MealWidget(this._meal); + + @override + Widget build(BuildContext context) { + return Container( + child: Card( + child: Column( + children: [ + DismissibleMealHeader(meal: _meal), + ..._meal.mealItems.map((item) => MealItemWidget(item)).toList(), + ], + ), + ), + ); + } +} + class MealItemWidget extends StatelessWidget { final MealItem _item; @@ -50,98 +70,89 @@ class MealItemWidget extends StatelessWidget { } } -class MealWidget extends StatelessWidget { - final Meal _meal; +class DismissibleMealHeader extends StatelessWidget { + const DismissibleMealHeader({ + Key key, + @required Meal meal, + }) : _meal = meal, + super(key: key); - MealWidget(this._meal); + final Meal _meal; @override Widget build(BuildContext context) { - return Container( - child: Card( - child: Column( + return Dismissible( + key: Key(_meal.id.toString()), + child: Container( + width: double.infinity, + decoration: BoxDecoration(color: Colors.black12), + padding: const EdgeInsets.all(10), + child: Text(_meal.time.format(context)), + ), + secondaryBackground: Container( + color: Theme.of(context).accentColor, + padding: EdgeInsets.only(right: 10), + child: Row( + mainAxisAlignment: MainAxisAlignment.end, children: [ - Dismissible( - key: Key(_meal.id.toString()), - child: Container( - width: double.infinity, - decoration: BoxDecoration(color: Colors.black12), - padding: const EdgeInsets.all(10), - child: Text(_meal.time.format(context)), - ), - secondaryBackground: Container( - color: Theme.of(context).accentColor, - padding: EdgeInsets.only(right: 10), - child: Row( - mainAxisAlignment: MainAxisAlignment.end, - children: [ - Icon( - Icons.delete, - color: Colors.white, - ), - Text( - DefaultMaterialLocalizations().deleteButtonTooltip, - style: TextStyle(color: Colors.white), - ), - ], - ), - ), - background: Container( - color: Theme.of(context).primaryColor, - alignment: Alignment.centerLeft, - padding: EdgeInsets.only(left: 10), - child: Row( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - Text( - 'Log this meal', - style: TextStyle(color: Colors.white), - ), - Icon( - Icons.bar_chart, - color: Colors.white, - ), - ], - ), - ), - confirmDismiss: (direction) async { - // Delete - if (direction == DismissDirection.endToStart) { - // Delete the meal - //Provider.of(context, listen: false).deleteMeal(_meal.id); - - // and inform the user - Scaffold.of(context).showSnackBar( - SnackBar( - content: Text( - 'Would delete meal ${_meal.id}', - textAlign: TextAlign.center, - ), - ), - ); - - // Log meal - } else { - showDialog( - context: context, - builder: (context) => AlertDialog( - content: Text('Would log this meal'), - actions: [ - TextButton( - child: Text("Close"), - onPressed: () => Navigator.of(context).pop(), - ), - ], - ), - ); - } - return false; - }, + Icon( + Icons.delete, + color: Colors.white, ), - ..._meal.mealItems.map((item) => MealItemWidget(item)).toList(), ], ), ), + background: Container( + color: Theme.of(context).primaryColor, + alignment: Alignment.centerLeft, + padding: EdgeInsets.only(left: 10), + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + Text( + 'Log this meal', + style: TextStyle(color: Colors.white), + ), + Icon( + Icons.bar_chart, + color: Colors.white, + ), + ], + ), + ), + confirmDismiss: (direction) async { + // Delete + if (direction == DismissDirection.endToStart) { + // Delete the meal + //Provider.of(context, listen: false).deleteMeal(_meal.id); + + // and inform the user + Scaffold.of(context).showSnackBar( + SnackBar( + content: Text( + 'Would delete meal ${_meal.id}', + textAlign: TextAlign.center, + ), + ), + ); + + // Log meal + } else { + showDialog( + context: context, + builder: (context) => AlertDialog( + content: Text('Would log this meal'), + actions: [ + TextButton( + child: Text("Close"), + onPressed: () => Navigator.of(context).pop(), + ), + ], + ), + ); + } + return false; + }, ); } }