Skip to content

Commit

Permalink
211: select image variant from srcset
Browse files Browse the repository at this point in the history
  • Loading branch information
NikoHadouken committed Feb 9, 2025
1 parent 9ef2253 commit 7f3a858
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/features/news/models/news_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class NewsModel {
String summary;
String content;
String? author;
String? image;
ImageSrcSet? image;
String type;
Division? division;
List<NewsCategory> categories;
Expand Down Expand Up @@ -37,7 +37,7 @@ class NewsModel {
summary: news.summary ?? 'Leere Zusammenfassung.',
content: news.body.content,
author: null,
image: news.featuredImage?.original.url,
image: news.featuredImage,
type: news.categories.firstOrNull?.label ?? '',
division: division,
categories: news.categories,
Expand Down
2 changes: 1 addition & 1 deletion lib/features/news/screens/news_detail_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class NewsDetailScreen extends StatelessWidget {
Widget featuredImage(NewsModel news) {
if (news.image != null) {
return CachedNetworkImage(
imageUrl: news.image!,
imageUrl: selectImageVariant(news.image!, 'wide'),
);
}
return Image.asset(getPlaceholderImage(news.id));
Expand Down
9 changes: 9 additions & 0 deletions lib/features/news/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ bool isCustomFilterSelected(
String getPlaceholderImage(String id) {
return 'assets/graphics/placeholders/placeholder_${int.parse(id) % 3 + 1}.jpg';
}

String selectImageVariant(ImageSrcSet image, String type) {
for (var variant in image.srcset) {
if (variant.type == type) {
return variant.url;
}
}
return image.original.url;
}
4 changes: 3 additions & 1 deletion lib/features/news/widgets/news_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ class NewsCard extends StatelessWidget {

DecorationImage featuredImage(NewsModel news) {
return DecorationImage(
image: news.image != null ? CachedNetworkImageProvider(news.image!) : AssetImage(getPlaceholderImage(news.id)),
image: news.image != null
? CachedNetworkImageProvider(selectImageVariant(news.image!, 'large'))
: AssetImage(getPlaceholderImage(news.id)),
fit: BoxFit.fitWidth,
);
}
Expand Down

0 comments on commit 7f3a858

Please sign in to comment.