Skip to content

Commit

Permalink
fix: 2 bugfixes for the tagline (empty content + image in error) (#5421)
Browse files Browse the repository at this point in the history
* If for some reason, there is no news, we shouldn't display the tagline at all

* If the image can't be loaded, we hide it
  • Loading branch information
g123k committed Jun 20, 2024
1 parent b6f2f5c commit 4deb0b4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class AppNews {
final AppNewsList news;
final AppNewsFeed feed;

bool get hasContent => news._news.isNotEmpty && feed.news.isNotEmpty;

@override
String toString() {
return 'AppNews{news: $news, feed: $feed}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class AppNewsProvider extends ChangeNotifier {

AppNewsState _state;

bool get hasContent => _state is AppNewsStateLoaded;
bool get hasContent =>
_state is AppNewsStateLoaded &&
(_state as AppNewsStateLoaded).content.hasContent;

Future<void> loadLatestNews({bool forceUpdate = false}) async {
_emit(const AppNewsStateLoading());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class _TagLineContentTitle extends StatelessWidget {
}
}

class _TagLineContentBody extends StatelessWidget {
class _TagLineContentBody extends StatefulWidget {
const _TagLineContentBody({
required this.message,
this.textColor,
Expand All @@ -248,42 +248,51 @@ class _TagLineContentBody extends StatelessWidget {
final Color? textColor;
final AppNewsImage? image;

@override
State<_TagLineContentBody> createState() => _TagLineContentBodyState();
}

class _TagLineContentBodyState extends State<_TagLineContentBody> {
bool _imageError = false;

@override
Widget build(BuildContext context) {
final ThemeProvider themeProvider = context.watch<ThemeProvider>();
final SmoothColorsThemeExtension theme =
Theme.of(context).extension<SmoothColorsThemeExtension>()!;

final Widget text = FormattedText(
text: message,
text: widget.message,
textStyle: TextStyle(
color: textColor ??
color: widget.textColor ??
(!themeProvider.isDarkMode(context)
? theme.primarySemiDark
: theme.primaryLight),
),
);

if (image == null) {
if (widget.image == null) {
return text;
}

final int imageFlex = ((image!.width ?? 0.2) * 10).toInt();
final int imageFlex = ((widget.image!.width ?? 0.2) * 10).toInt();
return Row(
children: <Widget>[
Expanded(
flex: imageFlex,
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.sizeOf(context).height * 0.06,
),
child: AspectRatio(
aspectRatio: 1.0,
child: _image(),
if (!_imageError) ...<Widget>[
Expanded(
flex: imageFlex,
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.sizeOf(context).height * 0.06,
),
child: AspectRatio(
aspectRatio: 1.0,
child: _image(),
),
),
),
),
const SizedBox(width: MEDIUM_SPACE),
const SizedBox(width: MEDIUM_SPACE),
],
Expanded(
flex: 10 - imageFlex,
child: text,
Expand All @@ -293,15 +302,28 @@ class _TagLineContentBody extends StatelessWidget {
}

Widget _image() {
if (image!.src.endsWith('svg')) {
if (widget.image!.src.endsWith('svg')) {
return SvgCache(
image!.src,
semanticsLabel: image!.alt,
widget.image!.src,
semanticsLabel: widget.image!.alt,
);
} else {
return Image.network(
semanticLabel: image!.alt,
image!.src,
semanticLabel: widget.image!.alt,
errorBuilder: (
BuildContext context,
Object error,
StackTrace? stackTrace,
) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (_imageError != true) {
setState(() => _imageError = true);
}
});

return EMPTY_WIDGET;
},
widget.image!.src,
);
}
}
Expand Down

0 comments on commit 4deb0b4

Please sign in to comment.