Skip to content

Commit fea019f

Browse files
committed
show source name and link to source, when possible in popup
1 parent 2ef68ce commit fea019f

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

lib/widgets/nutrition/helpers.dart

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import 'package:flutter/material.dart';
2020
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2121
import 'package:provider/provider.dart';
22+
import 'package:wger/helpers/misc.dart';
2223
import 'package:wger/models/nutrition/ingredient.dart';
2324
import 'package:wger/models/nutrition/meal.dart';
2425
import 'package:wger/models/nutrition/nutritional_goals.dart';
@@ -105,20 +106,25 @@ void showIngredientDetails(BuildContext context, int id, {String? image}) {
105106
// we also don't get an image when querying the API
106107
// however, the typeahead suggestion does get an image, so we allow passing it...
107108

108-
final url = context.read<NutritionPlansProvider>().baseProvider.auth.serverUrl;
109+
final serverURL = context.read<NutritionPlansProvider>().baseProvider.auth.serverUrl;
109110

110-
// TODO: display source name and source URL for an ingredient in the UI
111111
showDialog(
112112
context: context,
113113
builder: (context) => FutureBuilder<Ingredient>(
114114
future: Provider.of<NutritionPlansProvider>(context, listen: false).fetchIngredient(id),
115115
builder: (BuildContext context, AsyncSnapshot<Ingredient> snapshot) {
116116
Ingredient? ingredient;
117117
NutritionalGoals? goals;
118+
String? source;
119+
String? url;
118120

119121
if (snapshot.hasData) {
120122
ingredient = snapshot.data;
121123
goals = ingredient!.nutritionalValues.toGoals();
124+
source = ingredient.sourceName ?? 'unknown';
125+
url = ingredient.remoteId == null
126+
? null
127+
: 'https://world.openfoodfacts.org/product/${ingredient.remoteId}';
122128
}
123129
return AlertDialog(
124130
title: (snapshot.hasData) ? Text(ingredient!.name) : null,
@@ -128,7 +134,7 @@ void showIngredientDetails(BuildContext context, int id, {String? image}) {
128134
mainAxisSize: MainAxisSize.min,
129135
children: [
130136
if (image != null)
131-
CircleAvatar(backgroundImage: NetworkImage(url! + image), radius: 128),
137+
CircleAvatar(backgroundImage: NetworkImage(serverURL! + image), radius: 128),
132138
if (image != null) const SizedBox(height: 12),
133139
if (snapshot.hasError)
134140
Text(
@@ -145,6 +151,15 @@ void showIngredientDetails(BuildContext context, int id, {String? image}) {
145151
showGperKg: false,
146152
),
147153
),
154+
if (snapshot.hasData && url == null) Text('Source: ${source!}'),
155+
if (snapshot.hasData && url != null)
156+
Padding(
157+
padding: const EdgeInsets.only(top: 12),
158+
child: InkWell(
159+
child: Text('Source: ${source!}'),
160+
onTap: () => launchURL(url!, context),
161+
),
162+
),
148163
],
149164
),
150165
),

0 commit comments

Comments
 (0)