Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 4957 - SVG icons are now correctly refreshed when their URL change #5133

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ class AssetCacheHelper {

Exception loadException() =>
Exception('could not load any cached file ($cachedFilenames)');

/// Kind of [ObjectKey].
Key getKey() => Key('$url/$width/$height/$color/$cachedFilenames');
}
16 changes: 9 additions & 7 deletions packages/smooth_app/lib/cards/category_cards/svg_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ class SvgCache extends AbstractCache {
? Colors.white
: Colors.black;
}
final AssetCacheHelper helper = AssetCacheHelper(
cachedFilenames,
iconUrl!,
width: width,
height: height,
color: forcedColor,
);
return SvgSafeNetwork(
AssetCacheHelper(
cachedFilenames,
iconUrl!,
width: width,
height: height,
color: forcedColor,
),
helper,
key: helper.getKey(),
);
}

Expand Down
22 changes: 15 additions & 7 deletions packages/smooth_app/lib/cards/category_cards/svg_safe_network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import 'package:smooth_app/services/smooth_services.dart';
///
/// We could use SvgPicture.network, but it sends tons of errors if there in no
/// internet connection. That's why we download the data ourselves.
/// We do need the [key] for cases like "We updated the categories and then the
/// ecoscore changed, as an impact". Without a key, flutter may not refresh the
/// ecoscore svg widget when the URL changes.
/// cf. https://api.flutter.dev/flutter/foundation/Key-class.html
class SvgSafeNetwork extends StatefulWidget {
const SvgSafeNetwork(this.helper);
const SvgSafeNetwork(this.helper, {required super.key});

final AssetCacheHelper helper;

Expand Down Expand Up @@ -61,12 +65,16 @@ class _SvgSafeNetworkState extends State<SvgSafeNetwork> {
_networkCache[_url] = cached = response1.body;
return cached;
}
if (response1.statusCode == statusNotFound && alternateUrl != null) {
// try with the alternate url
final http.Response response2 = await http.get(Uri.parse(alternateUrl));
if (response2.statusCode == statusOk) {
_networkCache[alternateUrl] = cached = response2.body;
return cached;
if (response1.statusCode == statusNotFound) {
if (alternateUrl != null) {
// try with the alternate url
final http.Response response2 = await http.get(Uri.parse(alternateUrl));
if (response2.statusCode == statusOk) {
_networkCache[alternateUrl] = cached = response2.body;
return cached;
}
throw Exception(
'Failed to load SVG: $_url ${response1.statusCode} $alternateUrl ${response2.statusCode}');
}
}

Expand Down
Loading