svgIconImage method
This extension function is used to create an SvgPicture widget from a URL (if the string starts with "http") or from an asset path. If the string is a URL (starts with "http"), it creates an SvgPicture.network widget with the specified attributes. If there is an error loading the image, it shows a placeholder with a random color. If the string is not a URL, it assumes it's an asset path and creates an SvgPicture.asset widget with the specified attributes. If there is an error loading the image, it shows a placeholder with a random color.
Implementation
SvgPicture svgIconImage({
double? height,
double? width,
BoxFit? fit,
Color? color,
}) {
if (this.startsWith("http")) {
return SvgPicture.network(
this,
height: height ?? 24,
width: width ?? 24,
fit: fit ?? BoxFit.contain,
colorFilter:
color != null ? ColorFilter.mode(color, BlendMode.srcIn) : null,
placeholderBuilder: (context) => Placeholder(
color: imagePlaceholderColor[
Random().nextInt(imagePlaceholderColor.length)],
fallbackHeight: height ?? 24,
fallbackWidth: width ?? 24,
strokeWidth: 1,
child: SizedBox(
height: height ?? 24,
width: width ?? 24,
),
),
);
}
return SvgPicture.asset(
this,
height: height ?? 24,
width: width ?? 24,
fit: fit ?? BoxFit.cover,
colorFilter:
color != null ? ColorFilter.mode(color, BlendMode.srcIn) : null,
placeholderBuilder: (context) {
return Placeholder(
color: imagePlaceholderColor[
Random().nextInt(imagePlaceholderColor.length)],
fallbackHeight: height ?? 24,
fallbackWidth: width ?? 24,
strokeWidth: 1,
child: SizedBox(
height: height ?? 24,
width: width ?? 24,
),
);
},
);
}