Extract helper functions as own widgets

This commit is contained in:
Roland Geider committed 2026-05-08 12:51:07 +02:00
1 parent f4c8db73ee
commit 98737fde74
9 files changed
+435 -241

No files matched your search

@@ -0,0 +1,47 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 - 2026 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/l10n/generated/app_localizations.dart';
import 'package:wger/models/exercises/translation.dart';
import 'package:wger/widgets/core/core.dart';
class AliasesSection extends StatelessWidget {
final Translation translation;
const AliasesSection({super.key, required this.translation});
@override
Widget build(BuildContext context) {
if (translation.aliases.isEmpty) {
return const SizedBox.shrink();
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MutedText(
AppLocalizations.of(context).alsoKnownAs(
translation.aliases.map((e) => e.alias).toList().join(', '),
),
),
const SizedBox(height: 9),
],
);
}
}
@@ -0,0 +1,55 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 - 2026 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/helpers/i18n.dart';
import 'package:wger/models/exercises/exercise.dart';
class CategoryEquipmentRow extends StatelessWidget {
final Exercise exercise;
const CategoryEquipmentRow({super.key, required this.exercise});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Row(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: Chip(
label: Text(getServerStringTranslation(exercise.category!.name, context)),
padding: EdgeInsets.zero,
backgroundColor: theme.splashColor,
),
),
...exercise.equipment.map(
(e) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: Chip(
label: Text(getServerStringTranslation(e.name, context)),
padding: EdgeInsets.zero,
backgroundColor: theme.splashColor,
),
),
),
],
);
}
}
@@ -0,0 +1,42 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 - 2026 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_html/flutter_html.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/exercises/translation.dart';
class DescriptionSection extends StatelessWidget {
final Translation translation;
const DescriptionSection({super.key, required this.translation});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppLocalizations.of(context).description,
style: Theme.of(context).textTheme.headlineSmall,
),
Html(data: translation.description),
],
);
}
}
@@ -0,0 +1,42 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 - 2026 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/models/exercises/exercise.dart';
import 'package:wger/widgets/exercises/exercises.dart';
class ImagesSection extends StatelessWidget {
final Exercise exercise;
const ImagesSection({super.key, required this.exercise});
@override
Widget build(BuildContext context) {
if (exercise.images.isEmpty) {
return const SizedBox.shrink();
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CarouselImages(images: exercise.images),
const SizedBox(height: 9),
],
);
}
}
@@ -0,0 +1,83 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 - 2026 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/l10n/generated/app_localizations.dart';
import 'package:wger/models/exercises/exercise.dart';
import 'package:wger/widgets/exercises/exercises.dart';
class MusclesSection extends StatelessWidget {
final Exercise exercise;
const MusclesSection({super.key, required this.exercise});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppLocalizations.of(context).muscles,
style: Theme.of(context).textTheme.headlineSmall,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 9),
child: MuscleWidget(
muscles: exercise.muscles,
musclesSecondary: exercise.musclesSecondary,
isFront: true,
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 9),
child: MuscleWidget(
muscles: exercise.muscles,
musclesSecondary: exercise.musclesSecondary,
isFront: false,
),
),
),
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const MuscleColorHelper(main: true),
...exercise.muscles.map((e) => Text(e.nameTranslated(context))),
],
),
const SizedBox(height: 9),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const MuscleColorHelper(main: false),
...exercise.musclesSecondary.map((e) => Text(e.name)),
],
),
const SizedBox(height: 9),
],
);
}
}
@@ -0,0 +1,46 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 - 2026 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/l10n/generated/app_localizations.dart';
import 'package:wger/models/exercises/translation.dart';
class NotesSection extends StatelessWidget {
final Translation translation;
const NotesSection({super.key, required this.translation});
@override
Widget build(BuildContext context) {
if (translation.notes.isEmpty) {
return const SizedBox.shrink();
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppLocalizations.of(context).notes,
style: Theme.of(context).textTheme.headlineSmall,
),
...translation.notes.map((e) => Text(e.comment)),
const SizedBox(height: 9),
],
);
}
}
@@ -0,0 +1,58 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 - 2026 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/l10n/generated/app_localizations.dart';
import 'package:wger/models/exercises/exercise.dart';
import 'package:wger/providers/exercises_notifier.dart';
import 'package:wger/widgets/exercises/list_tile.dart';
class VariationsSection extends ConsumerWidget {
final Exercise exercise;
const VariationsSection({super.key, required this.exercise});
@override
Widget build(BuildContext context, WidgetRef ref) {
if (exercise.variationGroup == null) {
return const SizedBox.shrink();
}
final exerciseState = ref.watch(exercisesProvider).value ?? const ExerciseState([]);
final variations = exerciseState.findByVariationGroup(
exercise.variationGroup,
exerciseIdToExclude: exercise.id,
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppLocalizations.of(context).variations,
style: Theme.of(context).textTheme.headlineSmall,
),
if (variations.isEmpty)
const Text('-/-')
else
...variations.map((e) => ExerciseListTile(exercise: e)),
const SizedBox(height: 9),
],
);
}
}
@@ -0,0 +1,43 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 - 2026 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/helpers/platform.dart';
import 'package:wger/models/exercises/exercise.dart';
import 'package:wger/widgets/exercises/videos.dart';
class VideosSection extends StatelessWidget {
final Exercise exercise;
const VideosSection({super.key, required this.exercise});
@override
Widget build(BuildContext context) {
if (exercise.videos.isEmpty || isDesktop) {
return const SizedBox.shrink();
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
...exercise.videos.map((v) => ExerciseVideoWidget(video: v)),
const SizedBox(height: 9),
],
);
}
}
+19 -241
View File
@@ -18,35 +18,29 @@
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/helpers/i18n.dart';
import 'package:wger/helpers/platform.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/exercises/exercise.dart';
import 'package:wger/models/exercises/image.dart';
import 'package:wger/models/exercises/muscle.dart';
import 'package:wger/models/exercises/translation.dart';
import 'package:wger/providers/exercises_notifier.dart';
import 'package:wger/widgets/core/core.dart';
import 'package:wger/widgets/exercises/detail/aliases_section.dart';
import 'package:wger/widgets/exercises/detail/category_equipment_row.dart';
import 'package:wger/widgets/exercises/detail/description_section.dart';
import 'package:wger/widgets/exercises/detail/images_section.dart';
import 'package:wger/widgets/exercises/detail/muscles_section.dart';
import 'package:wger/widgets/exercises/detail/notes_section.dart';
import 'package:wger/widgets/exercises/detail/variations_section.dart';
import 'package:wger/widgets/exercises/detail/videos_section.dart';
import 'package:wger/widgets/exercises/images.dart';
import 'package:wger/widgets/exercises/list_tile.dart';
import 'package:wger/widgets/exercises/videos.dart';
class ExerciseDetail extends ConsumerWidget {
class ExerciseDetail extends StatelessWidget {
final Exercise _exercise;
static const PADDING = 9.0;
final CarouselController carouselController = CarouselController();
ExerciseDetail(this._exercise);
const ExerciseDetail(this._exercise, {super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
// Reactive: rebuild when the catalogue updates. Empty fallback while
// the stream hasn't emitted yet.
final exerciseState = ref.watch(exercisesProvider).value ?? const ExerciseState([]);
Widget build(BuildContext context) {
final translation = _exercise.getTranslation(Localizations.localeOf(context).languageCode);
return SingleChildScrollView(
@@ -54,234 +48,18 @@ class ExerciseDetail extends ConsumerWidget {
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Category and equipment
getCategoriesAndEquipment(context),
// Alternative names
...getAliases(context, translation),
// Videos
...getVideos(),
// Images
...getImages(),
// Description
...getDescription(context, translation),
// Notes
...getNotes(context, translation),
// Muscles
...getMuscles(context),
// Variants
...getVariations(context, exerciseState),
CategoryEquipmentRow(exercise: _exercise),
AliasesSection(translation: translation),
VideosSection(exercise: _exercise),
ImagesSection(exercise: _exercise),
DescriptionSection(translation: translation),
NotesSection(translation: translation),
MusclesSection(exercise: _exercise),
VariationsSection(exercise: _exercise),
],
),
);
}
List<Widget> getVariations(BuildContext context, ExerciseState exerciseState) {
final variations = exerciseState.findByVariationGroup(
_exercise.variationGroup,
exerciseIdToExclude: _exercise.id,
);
final List<Widget> out = [];
if (_exercise.variationGroup == null) {
return out;
}
out.add(
Text(
AppLocalizations.of(context).variations,
style: Theme.of(context).textTheme.headlineSmall,
),
);
for (final element in variations) {
out.add(ExerciseListTile(exercise: element));
}
if (variations.isEmpty) {
out.add(const Text('-/-'));
}
out.add(const SizedBox(height: PADDING));
return out;
}
List<Widget> getNotes(BuildContext context, Translation translation) {
final List<Widget> out = [];
if (translation.notes.isNotEmpty) {
out.add(
Text(
AppLocalizations.of(context).notes,
style: Theme.of(context).textTheme.headlineSmall,
),
);
for (final e in translation.notes) {
out.add(Text(e.comment));
}
out.add(const SizedBox(height: PADDING));
}
return out;
}
List<Widget> getMuscles(BuildContext context) {
final List<Widget> out = [];
out.add(
Text(
AppLocalizations.of(context).muscles,
style: Theme.of(context).textTheme.headlineSmall,
),
);
out.add(
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: PADDING),
child: MuscleWidget(
muscles: _exercise.muscles,
musclesSecondary: _exercise.musclesSecondary,
isFront: true,
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: PADDING),
child: MuscleWidget(
muscles: _exercise.muscles,
musclesSecondary: _exercise.musclesSecondary,
isFront: false,
),
),
),
],
),
);
out.add(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const MuscleColorHelper(main: true),
..._exercise.muscles.map((e) => Text(e.nameTranslated(context))),
],
),
);
out.add(const SizedBox(height: PADDING));
out.add(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const MuscleColorHelper(main: false),
..._exercise.musclesSecondary.map((e) => Text(e.name)),
],
),
);
out.add(const SizedBox(height: PADDING));
return out;
}
List<Widget> getDescription(BuildContext context, Translation translation) {
final List<Widget> out = [];
out.add(
Text(
AppLocalizations.of(context).description,
style: Theme.of(context).textTheme.headlineSmall,
),
);
out.add(Html(data: translation.description));
return out;
}
List<Widget> getImages() {
final List<Widget> out = [];
if (_exercise.images.isNotEmpty) {
out.add(
CarouselImages(
images: _exercise.images,
),
);
// out.add(ExerciseImageWidget(
// image: _exercise.getMainImage,
// height: 250,
// ));
out.add(const SizedBox(height: PADDING));
}
return out;
}
Widget getCategoriesAndEquipment(BuildContext context) {
final List<Widget> out = [];
final theme = Theme.of(context);
out.add(
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: Chip(
label: Text(getServerStringTranslation(_exercise.category!.name, context)),
padding: EdgeInsets.zero,
backgroundColor: theme.splashColor,
),
),
);
if (_exercise.equipment.isNotEmpty) {
_exercise.equipment
.map(
(e) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: Chip(
label: Text(getServerStringTranslation(e.name, context)),
padding: EdgeInsets.zero,
backgroundColor: theme.splashColor,
),
),
)
.forEach((element) => out.add(element));
}
out.add(const SizedBox(height: PADDING));
return Row(children: [...out]);
}
List<Widget> getVideos() {
// TODO: add carousel for the other videos
final List<Widget> out = [];
if (_exercise.videos.isNotEmpty && !isDesktop) {
_exercise.videos.map((v) => ExerciseVideoWidget(video: v)).forEach((element) {
out.add(element);
});
out.add(const SizedBox(height: PADDING));
}
return out;
}
List<Widget> getAliases(BuildContext context, Translation translation) {
final List<Widget> out = [];
if (translation.aliases.isNotEmpty) {
out.add(
MutedText(
AppLocalizations.of(context).alsoKnownAs(
translation.aliases.map((e) => e.alias).toList().join(', '),
),
),
);
out.add(const SizedBox(height: PADDING));
}
return out;
}
}
class MuscleColorHelper extends StatelessWidget {