mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 01:18:35 +01:00
feature(nutrition): refactor IngredientsScreen and create IngredientDetailScreen using existing components
This commit is contained in:
@@ -39,7 +39,7 @@ import 'package:wger/screens/form_screen.dart';
|
||||
import 'package:wger/screens/gallery_screen.dart';
|
||||
import 'package:wger/screens/gym_mode.dart';
|
||||
import 'package:wger/screens/home_tabs_screen.dart';
|
||||
import 'package:wger/screens/ingredient_screen.dart';
|
||||
import 'package:wger/screens/ingredient_detail_screen.dart';
|
||||
import 'package:wger/screens/ingredients_screen.dart';
|
||||
import 'package:wger/screens/log_meal_screen.dart';
|
||||
import 'package:wger/screens/log_meals_screen.dart';
|
||||
|
||||
@@ -51,8 +51,6 @@ final class IngredientNotifier extends _$IngredientNotifier {
|
||||
ref.keepAlive();
|
||||
_logger.finer('Building ingredient stream');
|
||||
_repo = ref.watch(ingredientRepositoryProvider);
|
||||
// Clear cache when the main list changes
|
||||
// ref.onDispose(() => _cache.clear());
|
||||
|
||||
return _repo.watchAllDrift();
|
||||
}
|
||||
|
||||
126
lib/screens/ingredient_detail_screen.dart
Normal file
126
lib/screens/ingredient_detail_screen.dart
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (c) 2025 wger Team
|
||||
*
|
||||
* wger Workout Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:wger/helpers/misc.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/nutrition/ingredient.dart';
|
||||
import 'package:wger/widgets/core/wger_image.dart';
|
||||
import 'package:wger/widgets/nutrition/ingredient_dialogs.dart';
|
||||
import 'package:wger/widgets/nutrition/macro_nutrients_table.dart';
|
||||
|
||||
class IngredientDetailScreen extends ConsumerWidget {
|
||||
static const routeName = '/ingredient-detail';
|
||||
|
||||
const IngredientDetailScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final ingredient = ModalRoute.of(context)!.settings.arguments as Ingredient;
|
||||
final goals = ingredient.nutritionalValues.toGoals();
|
||||
final source = ingredient.sourceName ?? 'unknown';
|
||||
|
||||
const placeholder = Image(
|
||||
image: AssetImage('assets/images/placeholder.png'),
|
||||
color: Color.fromRGBO(255, 255, 255, 0.3),
|
||||
colorBlendMode: BlendMode.modulate,
|
||||
semanticLabel: 'Placeholder',
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
body: CustomScrollView(
|
||||
slivers: [
|
||||
SliverAppBar(
|
||||
expandedHeight: 250.0,
|
||||
pinned: true,
|
||||
flexibleSpace: FlexibleSpaceBar(
|
||||
title: Text(
|
||||
ingredient.name,
|
||||
style: const TextStyle(
|
||||
shadows: [Shadow(color: Colors.black45, blurRadius: 4)],
|
||||
),
|
||||
),
|
||||
background: WgerImage(
|
||||
mediaPath: ingredient.image?.image,
|
||||
fit: BoxFit.cover,
|
||||
errorWidget: placeholder,
|
||||
),
|
||||
),
|
||||
),
|
||||
SliverPadding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
sliver: SliverList(
|
||||
delegate: SliverChildListDelegate([
|
||||
// Dietary Info (Vegan, Veg, NutriScore)
|
||||
Card(
|
||||
elevation: 2,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: DietaryInfoSection(ingredient: ingredient),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
Text(
|
||||
AppLocalizations.of(context).macronutrients,
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// Macronutrients Table
|
||||
Card(
|
||||
elevation: 2,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: MacronutrientsTable(
|
||||
nutritionalGoals: goals,
|
||||
plannedValuesPercentage: goals.energyPercentage(),
|
||||
showGperKg: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
Divider(color: Theme.of(context).colorScheme.outline),
|
||||
const SizedBox(height: 8),
|
||||
if (ingredient.licenseObjectURl == null)
|
||||
Text('Source: $source', style: Theme.of(context).textTheme.bodySmall)
|
||||
else
|
||||
InkWell(
|
||||
onTap: () => launchURL(ingredient.licenseObjectURl!, context),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: Text(
|
||||
'Source: $source',
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 40),
|
||||
]),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (c) 2025 wger Team
|
||||
*
|
||||
* wger Workout Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wger/core/wide_screen_wrapper.dart';
|
||||
import 'package:wger/models/nutrition/ingredient.dart';
|
||||
import 'package:wger/widgets/nutrition/ingredient_detail.dart';
|
||||
|
||||
class IngredientDetailScreen extends StatelessWidget {
|
||||
static const routeName = '/ingredient-detail';
|
||||
|
||||
const IngredientDetailScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Extract the ingredient from the route arguments
|
||||
final ingredient = ModalRoute.of(context)!.settings.arguments as Ingredient;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(ingredient.name),
|
||||
actions: const [],
|
||||
),
|
||||
body: WidescreenWrapper(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10),
|
||||
child: IngredientDetail(ingredient),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,7 @@ class IngredientDetails extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_DietaryInfoSection(ingredient: ingredient),
|
||||
DietaryInfoSection(ingredient: ingredient),
|
||||
if (ingredient.licenseObjectURl == null)
|
||||
Text('Source: $source')
|
||||
else
|
||||
@@ -283,10 +283,10 @@ class IngredientScanResultDialog extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
class _DietaryInfoSection extends StatelessWidget {
|
||||
class DietaryInfoSection extends StatelessWidget {
|
||||
final Ingredient ingredient;
|
||||
|
||||
const _DietaryInfoSection({required this.ingredient});
|
||||
const DietaryInfoSection({required this.ingredient});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -18,12 +18,9 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/core/search_options.dart';
|
||||
import 'package:wger/models/nutrition/ingredient.dart';
|
||||
import 'package:wger/providers/ingredient_filters_notifier.dart';
|
||||
import 'package:wger/providers/ingredient_repository.dart';
|
||||
import 'package:wger/widgets/nutrition/ingredient_filter_dialog.dart';
|
||||
|
||||
class IngredientFilterRow extends ConsumerStatefulWidget {
|
||||
const IngredientFilterRow({super.key});
|
||||
@@ -109,229 +106,3 @@ class _IngredientFilterRowState extends ConsumerState<IngredientFilterRow> {
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
// exact code for components from main branch's ingredients search in [IngredientTypeahead]
|
||||
|
||||
/// Filter dialog for the ingredient search. Shown via [showDialog] from the
|
||||
/// filter icon button in the [IngredientsScreen].
|
||||
///
|
||||
/// All state is owned by [ingredientFiltersProvider]; this widget is purely a
|
||||
/// view over that state.
|
||||
class IngredientFilterDialog extends ConsumerWidget {
|
||||
const IngredientFilterDialog({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final i18n = AppLocalizations.of(context);
|
||||
|
||||
return AlertDialog(
|
||||
title: Text(i18n.filter),
|
||||
content: const SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_LanguageDropdown(),
|
||||
_DietSwitches(),
|
||||
_NutriscoreSliderSection(),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: Text(MaterialLocalizations.of(context).closeButtonLabel),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps the shared [SearchLanguageDropdown] and binds it to
|
||||
/// [ingredientFiltersProvider].
|
||||
class _LanguageDropdown extends ConsumerWidget {
|
||||
const _LanguageDropdown();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final filters = ref.watch(ingredientFiltersSyncProvider);
|
||||
return SearchLanguageDropdown(
|
||||
selected: filters.searchLanguage,
|
||||
onChanged: (val) => ref.read(ingredientFiltersProvider.notifier).chooseLanguage(val),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Vegan + Vegetarian toggles, grouped because they are thematically and
|
||||
/// visually paired.
|
||||
class _DietSwitches extends ConsumerWidget {
|
||||
const _DietSwitches();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final i18n = AppLocalizations.of(context);
|
||||
final filters = ref.watch(ingredientFiltersSyncProvider);
|
||||
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SwitchListTile(
|
||||
title: Text(i18n.isVegan),
|
||||
value: filters.isVegan,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
onChanged: (val) => ref.read(ingredientFiltersProvider.notifier).toggleVegan(val),
|
||||
),
|
||||
SwitchListTile(
|
||||
title: Text(i18n.isVegetarian),
|
||||
value: filters.isVegetarian,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
onChanged: (val) => ref.read(ingredientFiltersProvider.notifier).toggleVegetarian(val),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Section that wraps [_NutriscoreSlider] and binds it to
|
||||
/// [ingredientFiltersProvider].
|
||||
class _NutriscoreSliderSection extends ConsumerWidget {
|
||||
const _NutriscoreSliderSection();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final filters = ref.watch(ingredientFiltersSyncProvider);
|
||||
return _NutriscoreSlider(
|
||||
value: filters.nutriscoreMax,
|
||||
onChanged: (grade) => ref.read(ingredientFiltersProvider.notifier).chooseNutriscoreMax(grade),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Discrete slider that lets the user pick the worst acceptable [NutriScore]
|
||||
/// grade. Index 0 is the "Off" position (no filter, `null` value) and
|
||||
/// indices 1..N map to [NutriScore.values].
|
||||
class _NutriscoreSlider extends StatelessWidget {
|
||||
final NutriScore? value;
|
||||
final ValueChanged<NutriScore?> onChanged;
|
||||
|
||||
const _NutriscoreSlider({required this.value, required this.onChanged});
|
||||
|
||||
static const int _offIndex = 0;
|
||||
|
||||
int _valueToIndex(NutriScore? v) => v == null ? _offIndex : NutriScore.values.indexOf(v) + 1;
|
||||
|
||||
NutriScore? _indexToValue(int i) => i == _offIndex ? null : NutriScore.values[i - 1];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final i18n = AppLocalizations.of(context);
|
||||
final index = _valueToIndex(value);
|
||||
final maxIndex = NutriScore.values.length; // 0=Off, then A..E
|
||||
final helperText = value == null
|
||||
? i18n.filterNutriscoreNoFilter
|
||||
: i18n.filterNutriscoreOrBetter(value!.name.toUpperCase());
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
i18n.filterNutriscore,
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
Text(
|
||||
helperText,
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
Slider(
|
||||
value: index.toDouble(),
|
||||
min: 0,
|
||||
max: maxIndex.toDouble(),
|
||||
divisions: maxIndex,
|
||||
label: value == null ? i18n.filterNutriscoreOff : value!.name.toUpperCase(),
|
||||
onChanged: (v) => onChanged(_indexToValue(v.round())),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
i18n.filterNutriscoreOff,
|
||||
style: Theme.of(context).textTheme.labelSmall,
|
||||
),
|
||||
...NutriScore.values.map(
|
||||
(score) => Text(
|
||||
score.name.toUpperCase(),
|
||||
style: Theme.of(context).textTheme.labelSmall,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Dropdown that lets the user pick which language(s) to search in.
|
||||
///
|
||||
/// Shared between exercise and ingredient search filters. The widget is
|
||||
/// purely presentational — the caller passes the currently [selected] value
|
||||
/// and an [onChanged] callback to react to user input.
|
||||
class SearchLanguageDropdown extends StatelessWidget {
|
||||
final SearchLanguage selected;
|
||||
final ValueChanged<SearchLanguage> onChanged;
|
||||
|
||||
const SearchLanguageDropdown({
|
||||
super.key,
|
||||
required this.selected,
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final i18n = AppLocalizations.of(context);
|
||||
final languageCode = Localizations.localeOf(context).languageCode;
|
||||
final isEnglish = languageCode == LANGUAGE_SHORT_ENGLISH;
|
||||
|
||||
final items = <DropdownMenuItem<SearchLanguage>>[
|
||||
DropdownMenuItem(
|
||||
value: SearchLanguage.current,
|
||||
child: Text(i18n.searchLanguageCurrent(languageCode)),
|
||||
),
|
||||
if (!isEnglish)
|
||||
DropdownMenuItem(
|
||||
value: SearchLanguage.currentAndEnglish,
|
||||
child: Text(i18n.searchLanguageEnglish(languageCode)),
|
||||
),
|
||||
DropdownMenuItem(
|
||||
value: SearchLanguage.all,
|
||||
child: Text(i18n.searchLanguageAll),
|
||||
),
|
||||
];
|
||||
|
||||
// If the persisted value isn't offered for the current locale (e.g.
|
||||
// currentAndEnglish on an English locale), fall back to null so the
|
||||
// dropdown shows a valid state until the auto-switch corrects it.
|
||||
SearchLanguage? value = selected;
|
||||
if (!items.any((it) => it.value == value)) {
|
||||
value = null;
|
||||
}
|
||||
|
||||
return ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text(i18n.language),
|
||||
subtitle: DropdownButton<SearchLanguage>(
|
||||
value: value,
|
||||
isExpanded: true,
|
||||
items: items,
|
||||
onChanged: (val) {
|
||||
if (val != null) {
|
||||
onChanged(val);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wger/models/nutrition/ingredient.dart';
|
||||
import 'package:wger/screens/ingredient_screen.dart';
|
||||
import 'package:wger/screens/ingredient_detail_screen.dart';
|
||||
import 'package:wger/widgets/nutrition/ingredient_images.dart';
|
||||
|
||||
class IngredientListTile extends StatelessWidget {
|
||||
@@ -53,7 +53,11 @@ class IngredientListTile extends StatelessWidget {
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.pushNamed(context, IngredientDetailScreen.routeName, arguments: ingredient);
|
||||
Navigator.pushNamed(
|
||||
context,
|
||||
IngredientDetailScreen.routeName,
|
||||
arguments: ingredient,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user