getImage static method
dynamic
getImage(})
Get an image widget from any image type
Implementation
static dynamic getImage(String? url, bool animate,
{Scope? scope,
Color? color,
String? defaultImage,
double? width,
double? height,
String? fit,
String? filter,
bool fade = true,
int? fadeDuration}) {
Widget? image;
try {
// parse the url
Uri? uri = URI.parse(url);
// bad url?
if (uri == null) {
if (defaultImage != null) {
if (defaultImage.toLowerCase().trim() == 'none') {
return Container();
} else {
return getImage(defaultImage, animate,
defaultImage: null,
fit: fit,
width: width,
height: height,
filter: filter,
fade: fade,
fadeDuration: fadeDuration);
}
}
return const Icon(Icons.broken_image_outlined,
size: 36, color: Colors.grey);
}
// error handler
Widget errorHandler(
BuildContext content, Object object, StackTrace? stacktrace) {
Log().debug(
"Bad image url (${url?.substring(0, min(100, url.length))}. Error is $object",
caller: "errorHandler");
if (defaultImage == null) {
return const Icon(Icons.broken_image_outlined,
size: 36, color: Colors.grey);
}
if (defaultImage.toLowerCase().trim() == 'none') return Container();
return getImage(defaultImage, animate,
defaultImage: null,
fit: fit,
width: width,
height: height,
filter: filter,
fade: fade,
fadeDuration: fadeDuration);
}
// get image type
switch (uri.scheme) {
/// data uri
case "data":
if (uri.data != null) {
image = FadeInImage(
placeholder: MemoryImage(placeholder),
image: MemoryImage(uri.data!.contentAsBytes()),
fit: getFit(fit),
width: width,
height: height,
fadeInDuration: Duration(milliseconds: fadeDuration ?? 300),
imageErrorBuilder: errorHandler);
}
break;
/// blob image from camera or file picker
case "blob":
image = FmlEngine.isWeb
? Image.network(url!, fit: getFit(fit))
: Image.file(File(url!), fit: getFit(fit));
break;
/// file image
case "file":
// file picker and camera return uri references as file:C:/...?
dynamic file = Platform.getFile(url!.replaceFirst("file:", ""));
// user defined local files?
file ??= Platform.getFile(uri.asFilePath());
// no file found
if (file == null) break;
// svg image?
if (uri.pageExtension == "svg") {
image = SvgPicture.file(file!,
fit: getFit(fit),
width: width,
height: height,
colorFilter: color != null
? ColorFilter.mode(color, BlendMode.srcIn)
: null);
} else {
image = Image.file(file, fit: getFit(fit));
}
break;
/// asset image
case "assets":
var assetpath = "${uri.scheme}/${uri.host}${uri.path}";
// svg image?
if (uri.pageExtension == "svg") {
image = SvgPicture.asset(assetpath,
fit: getFit(fit),
width: width,
height: height,
colorFilter: color != null
? ColorFilter.mode(color, BlendMode.srcIn)
: null);
} else {
image = Image.asset(assetpath,
fit: getFit(fit),
width: width,
height: height,
errorBuilder: errorHandler);
}
break;
/// web image
default:
if (uri.pageExtension == "svg") {
image = SvgPicture.network(uri.url,
fit: getFit(fit),
width: width,
height: height,
colorFilter: color != null
? ColorFilter.mode(color, BlendMode.srcIn)
: null);
} else {
if (animate) {
image = FadeInImage.memoryNetwork(
placeholder: placeholder,
image: uri.url,
fit: getFit(fit),
width: width,
height: height,
fadeInDuration: Duration(milliseconds: fadeDuration ?? 300),
imageErrorBuilder: errorHandler,
);
} else {
image = Image.network(uri.url,
fit: getFit(fit), width: width, height: height);
}
}
break;
}
} catch (e) {
Log().error("Error decoding image from $url. Error is $e");
}
// return widget
return image ??
Image.memory(placeholder,
fit: getFit(fit), width: width, height: height);
}