|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; |
| 3 | +import 'package:wger/helpers/misc.dart'; |
| 4 | +import 'package:wger/models/nutrition/ingredient.dart'; |
| 5 | +import 'package:wger/models/nutrition/nutritional_goals.dart'; |
| 6 | +import 'package:wger/widgets/nutrition/macro_nutrients_table.dart'; |
| 7 | + |
| 8 | +Widget ingredientImage(String url, BuildContext context) { |
| 9 | + var radius = 100.0; |
| 10 | + final height = MediaQuery.sizeOf(context).height; |
| 11 | + final width = MediaQuery.sizeOf(context).width; |
| 12 | + final smallest = height < width ? height : width; |
| 13 | + if (smallest < 400) { |
| 14 | + radius = smallest / 4; |
| 15 | + } |
| 16 | + return Padding( |
| 17 | + padding: const EdgeInsets.only(bottom: 12), |
| 18 | + child: CircleAvatar(backgroundImage: NetworkImage(url), radius: radius), |
| 19 | + ); |
| 20 | +} |
| 21 | + |
| 22 | +class IngredientDetails extends StatelessWidget { |
| 23 | + final AsyncSnapshot<Ingredient> snapshot; |
| 24 | + final void Function()? select; |
| 25 | + const IngredientDetails(this.snapshot, {super.key, this.select}); |
| 26 | + |
| 27 | + @override |
| 28 | + Widget build(BuildContext context) { |
| 29 | + Ingredient? ingredient; |
| 30 | + NutritionalGoals? goals; |
| 31 | + String? source; |
| 32 | + |
| 33 | + if (snapshot.hasData) { |
| 34 | + ingredient = snapshot.data; |
| 35 | + goals = ingredient!.nutritionalValues.toGoals(); |
| 36 | + source = ingredient.sourceName ?? 'unknown'; |
| 37 | + } |
| 38 | + |
| 39 | + return AlertDialog( |
| 40 | + title: (snapshot.hasData) ? Text(ingredient!.name) : null, |
| 41 | + content: SingleChildScrollView( |
| 42 | + child: Padding( |
| 43 | + padding: const EdgeInsets.all(8.0), |
| 44 | + child: Column( |
| 45 | + mainAxisSize: MainAxisSize.min, |
| 46 | + children: [ |
| 47 | + if (snapshot.hasError) |
| 48 | + Text( |
| 49 | + 'Ingredient lookup error: ${snapshot.error ?? 'unknown error'}', |
| 50 | + style: const TextStyle(color: Colors.red), |
| 51 | + ), |
| 52 | + if (ingredient?.image?.image != null) |
| 53 | + ingredientImage(ingredient!.image!.image, context), |
| 54 | + if (!snapshot.hasData && !snapshot.hasError) const CircularProgressIndicator(), |
| 55 | + if (snapshot.hasData) |
| 56 | + ConstrainedBox( |
| 57 | + constraints: const BoxConstraints(minWidth: 400), |
| 58 | + child: MacronutrientsTable( |
| 59 | + nutritionalGoals: goals!, |
| 60 | + plannedValuesPercentage: goals.energyPercentage(), |
| 61 | + showGperKg: false, |
| 62 | + ), |
| 63 | + ), |
| 64 | + if (snapshot.hasData && ingredient!.licenseObjectURl == null) |
| 65 | + Text('Source: ${source!}'), |
| 66 | + if (snapshot.hasData && ingredient!.licenseObjectURl != null) |
| 67 | + Padding( |
| 68 | + padding: const EdgeInsets.only(top: 12), |
| 69 | + child: InkWell( |
| 70 | + child: Text('Source: ${source!}'), |
| 71 | + onTap: () => launchURL(ingredient!.licenseObjectURl!, context), |
| 72 | + ), |
| 73 | + ), |
| 74 | + ], |
| 75 | + ), |
| 76 | + ), |
| 77 | + ), |
| 78 | + actions: [ |
| 79 | + if (snapshot.hasData && select != null) |
| 80 | + TextButton( |
| 81 | + key: const Key('ingredient-details-continue-button'), |
| 82 | + child: Text(MaterialLocalizations.of(context).continueButtonLabel), |
| 83 | + onPressed: () { |
| 84 | + select!(); |
| 85 | + Navigator.of(context).pop(); |
| 86 | + }, |
| 87 | + ), |
| 88 | + TextButton( |
| 89 | + key: const Key('ingredient-details-close-button'), |
| 90 | + child: Text(MaterialLocalizations.of(context).closeButtonLabel), |
| 91 | + onPressed: () { |
| 92 | + Navigator.of(context).pop(); |
| 93 | + }, |
| 94 | + ), |
| 95 | + ], |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +class IngredientScanResultDialog extends StatelessWidget { |
| 101 | + final AsyncSnapshot<Ingredient?> snapshot; |
| 102 | + final String barcode; |
| 103 | + final Function(int id, String name, num? amount) selectIngredient; |
| 104 | + |
| 105 | + const IngredientScanResultDialog(this.snapshot, this.barcode, this.selectIngredient, {super.key}); |
| 106 | + |
| 107 | + @override |
| 108 | + Widget build(BuildContext context) { |
| 109 | + Ingredient? ingredient; |
| 110 | + NutritionalGoals? goals; |
| 111 | + String? title; |
| 112 | + String? source; |
| 113 | + |
| 114 | + if (snapshot.connectionState == ConnectionState.done) { |
| 115 | + ingredient = snapshot.data; |
| 116 | + title = ingredient != null |
| 117 | + ? AppLocalizations.of(context).productFound |
| 118 | + : AppLocalizations.of(context).productNotFound; |
| 119 | + if (ingredient != null) { |
| 120 | + goals = ingredient.nutritionalValues.toGoals(); |
| 121 | + source = ingredient.sourceName ?? 'unknown'; |
| 122 | + } |
| 123 | + } |
| 124 | + return AlertDialog( |
| 125 | + key: const Key('ingredient-scan-result-dialog'), |
| 126 | + title: title != null ? Text(title) : null, |
| 127 | + content: SingleChildScrollView( |
| 128 | + child: Padding( |
| 129 | + padding: const EdgeInsets.all(8.0), |
| 130 | + child: Column( |
| 131 | + mainAxisSize: MainAxisSize.min, |
| 132 | + children: [ |
| 133 | + if (snapshot.hasError) |
| 134 | + Text( |
| 135 | + 'Ingredient lookup error: ${snapshot.error ?? 'unknown error'}', |
| 136 | + style: const TextStyle(color: Colors.red), |
| 137 | + ), |
| 138 | + if (snapshot.connectionState == ConnectionState.done && ingredient == null) |
| 139 | + Padding( |
| 140 | + padding: const EdgeInsets.only(bottom: 8.0), |
| 141 | + child: Text( |
| 142 | + AppLocalizations.of(context).productNotFoundDescription(barcode), |
| 143 | + ), |
| 144 | + ), |
| 145 | + if (ingredient != null) |
| 146 | + Padding( |
| 147 | + padding: const EdgeInsets.only(bottom: 8.0), |
| 148 | + child: |
| 149 | + Text(AppLocalizations.of(context).productFoundDescription(ingredient.name)), |
| 150 | + ), |
| 151 | + if (ingredient?.image?.image != null) |
| 152 | + ingredientImage(ingredient!.image!.image, context), |
| 153 | + if (snapshot.connectionState != ConnectionState.done && !snapshot.hasError) |
| 154 | + const CircularProgressIndicator(), |
| 155 | + if (goals != null) |
| 156 | + ConstrainedBox( |
| 157 | + constraints: const BoxConstraints(minWidth: 400), |
| 158 | + child: MacronutrientsTable( |
| 159 | + nutritionalGoals: goals, |
| 160 | + plannedValuesPercentage: goals.energyPercentage(), |
| 161 | + showGperKg: false, |
| 162 | + ), |
| 163 | + ), |
| 164 | + if (ingredient != null && ingredient.licenseObjectURl == null) |
| 165 | + Text('Source: ${source!}'), |
| 166 | + if (ingredient?.licenseObjectURl != null) |
| 167 | + Padding( |
| 168 | + padding: const EdgeInsets.only(top: 12), |
| 169 | + child: InkWell( |
| 170 | + child: Text('Source: ${source!}'), |
| 171 | + onTap: () => launchURL(ingredient!.licenseObjectURl!, context), |
| 172 | + ), |
| 173 | + ), |
| 174 | + ], |
| 175 | + ), |
| 176 | + ), |
| 177 | + ), |
| 178 | + actions: [ |
| 179 | + if (ingredient != null) // if barcode matched |
| 180 | + TextButton( |
| 181 | + key: const Key('ingredient-scan-result-dialog-confirm-button'), |
| 182 | + child: Text(MaterialLocalizations.of(context).continueButtonLabel), |
| 183 | + onPressed: () { |
| 184 | + selectIngredient(ingredient!.id, ingredient.name, null); |
| 185 | + Navigator.of(context).pop(); |
| 186 | + }, |
| 187 | + ), |
| 188 | + // if didn't match, or we're still waiting |
| 189 | + TextButton( |
| 190 | + key: const Key('ingredient-scan-result-dialog-close-button'), |
| 191 | + child: Text(MaterialLocalizations.of(context).closeButtonLabel), |
| 192 | + onPressed: () { |
| 193 | + Navigator.of(context).pop(); |
| 194 | + }, |
| 195 | + ), |
| 196 | + ], |
| 197 | + ); |
| 198 | + } |
| 199 | +} |
0 commit comments