Files
feeddeck/app/lib/widgets/utils/cached_network_image.dart
Rico Berger 5753fb2714 Update Flutter to Version 3.16.5 (#106)
Update the used Flutter version to 3.16.5 and the used packages to their
latest version.

The Supabase package contained some breaking changes:
- `functionUrl` is not exported anymore, so that it must be generated by
  ourselfs
- `Provider` was renamed to `OAuthProvider`
- The `signInWithApple` method was removed and is now implemented by us
  via the `sign_in_with_apple` package.

We also renamed the `DesktopLoginManager` to `DesktopSignInManager` to
use the same naming as in other places of the app, where we are always
using sign in and not login.
2023-12-21 17:24:07 +01:00

81 lines
2.6 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart' as cni;
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:feeddeck/repositories/settings_repository.dart';
/// [getImageUrl] returns the "correct" image url for the provided [imageUrl].
/// "Correct" means that depending on the provided [imageUrl] and the current
/// platform, the image url will be pointed to the Supabase storage or will be
/// proxied via the "image-proxy-v1" Supabase function.
String getImageUrl(String imageUrl) {
if (imageUrl.startsWith('http://') || imageUrl.startsWith('https://')) {
if (kIsWeb) {
return '${SettingsRepository().supabaseUrl}/functions/v1/image-proxy-v1?media=${Uri.encodeQueryComponent(imageUrl)}';
}
return imageUrl;
}
return '${SettingsRepository().supabaseUrl}/storage/v1/object/public/sources/$imageUrl';
}
/// The [CachedNetworkImage] is a wrapper around the [cni.CachedNetworkImage]
/// widget, which will automatically use the correct url to display the image,
/// via the [getImageUrl] function.
class CachedNetworkImage extends StatelessWidget {
final String imageUrl;
final double? width;
final double? height;
final BoxFit? fit;
final Widget Function(BuildContext, String)? placeholder;
final Widget Function(BuildContext, String, Object)? errorWidget;
const CachedNetworkImage({
super.key,
required this.imageUrl,
this.width,
this.height,
this.fit,
this.placeholder,
this.errorWidget,
});
@override
Widget build(BuildContext context) {
return cni.CachedNetworkImage(
cacheManager: CustomCacheManager(),
imageUrl: getImageUrl(imageUrl),
width: width,
height: height,
fit: fit,
placeholder: placeholder,
errorWidget: errorWidget,
);
}
}
/// [CustomCacheManager] is a custom [CacheManager] which is used by the
/// [CachedNetworkImage] widget to cache the images. This is required to adjust
/// the `stalePeriod` to 7 days, instead of the default 30 days. 7 days should
/// be enough for our use case and will reduce the storage usage.
class CustomCacheManager extends CacheManager with ImageCacheManager {
static const key = 'libCachedImageData';
static final CustomCacheManager _instance = CustomCacheManager._internal();
factory CustomCacheManager() {
return _instance;
}
CustomCacheManager._internal()
: super(
Config(
key,
stalePeriod: const Duration(days: 7),
),
);
}