spotube/lib/components/shared/image/universal_image.dart
Kingkor Roy Tirtho 6673e5a8a8
feat: improved caching based on riverpod (#1343)
* feat: add riverpod based favorite album provider

* feat: add album is saved, new releases and tracks providers

* feat: add artist related providers

* feat: add all categories providers

* feat: add lyrics provider

* feat: add playlist related providers

* feat: add search provider

* feat: add view and spotify friends provider

* feat: add playlist create and update and favorite handlers

* feat: use providers in home screen

* chore: fix dart lint issues

* feat: use new providers for playlist and albums screen

* feat: use providers in artist page

* feat: use providers on library page

* feat: use provider for playlist and album card and heart button

* feat: use provider in search page

* feat: use providers in generate playlist

* feat: use provider in lyrics screen

* feat: use provider for create playlist

* feat: use provider in add track dialog

* feat: use providers in remaining pages and remove fl_query

* fix: remove direct access to provider.value

* fix: glitching when loading

* fix: user album loading next page indicator

* feat: make many provider autoDispose after 5 minutes of no usage

* fix: ignore episodes in tracks
2024-03-20 23:38:39 +06:00

137 lines
3.6 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:spotube/collections/assets.gen.dart';
class UniversalImage extends HookWidget {
final String path;
final double? height;
final double? width;
final double scale;
final String? placeholder;
final BoxFit? fit;
const UniversalImage({
required this.path,
this.height,
this.width,
this.placeholder,
this.fit,
this.scale = 1,
super.key,
});
static ImageProvider imageProvider(
String path, {
final double? height,
final double? width,
final double scale = 1,
}) {
if (path.startsWith("http")) {
return CachedNetworkImageProvider(
path,
maxHeight: height?.toInt(),
maxWidth: width?.toInt(),
cacheKey: path,
scale: scale,
);
} else if (path.startsWith("assets/")) {
return AssetImage(path);
} else if (Uri.tryParse(path) != null) {
return FileImage(File(path), scale: scale);
}
return MemoryImage(base64Decode(path), scale: scale);
}
@override
Widget build(BuildContext context) {
if (path.startsWith("http")) {
return FadeInImage(
image: CachedNetworkImageProvider(
path,
maxHeight: height?.toInt(),
maxWidth: width?.toInt(),
cacheKey: path,
scale: scale,
),
height: height,
width: width,
placeholder: AssetImage(placeholder ?? Assets.placeholder.path),
imageErrorBuilder: (context, error, stackTrace) {
return Image.asset(
placeholder ?? Assets.placeholder.path,
width: width,
height: height,
cacheHeight: height?.toInt(),
cacheWidth: width?.toInt(),
scale: scale,
);
},
fit: fit,
);
} else if (Uri.tryParse(path) != null && !path.startsWith("assets")) {
return Image.file(
File(path),
width: width,
height: height,
cacheHeight: height?.toInt(),
cacheWidth: width?.toInt(),
scale: scale,
fit: fit,
errorBuilder: (context, error, stackTrace) {
return Image.asset(
placeholder ?? Assets.placeholder.path,
width: width,
height: height,
cacheHeight: height?.toInt(),
cacheWidth: width?.toInt(),
scale: scale,
);
},
);
} else if (path.startsWith("assets")) {
return Image.asset(
path,
width: width,
height: height,
cacheHeight: height?.toInt(),
cacheWidth: width?.toInt(),
scale: scale,
fit: fit,
errorBuilder: (context, error, stackTrace) {
return Image.asset(
placeholder ?? Assets.placeholder.path,
width: width,
height: height,
cacheHeight: height?.toInt(),
cacheWidth: width?.toInt(),
scale: scale,
);
},
);
}
return Image.memory(
base64Decode(path),
width: width,
height: height,
cacheHeight: height?.toInt(),
cacheWidth: width?.toInt(),
scale: scale,
fit: fit,
errorBuilder: (context, error, stackTrace) {
return Image.asset(
placeholder ?? Assets.placeholder.path,
width: width,
height: height,
cacheHeight: height?.toInt(),
cacheWidth: width?.toInt(),
scale: scale,
);
},
);
}
}