mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 07:55:18 +00:00
121 lines
3.2 KiB
Dart
121 lines
3.2 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;
|
|
const UniversalImage({
|
|
required this.path,
|
|
this.height,
|
|
this.width,
|
|
this.placeholder,
|
|
this.scale = 1,
|
|
Key? key,
|
|
}) : super(key: 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),
|
|
);
|
|
} 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,
|
|
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,
|
|
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,
|
|
errorBuilder: (context, error, stackTrace) {
|
|
return Image.asset(
|
|
placeholder ?? Assets.placeholder.path,
|
|
width: width,
|
|
height: height,
|
|
cacheHeight: height?.toInt(),
|
|
cacheWidth: width?.toInt(),
|
|
scale: scale,
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|