iconImage method

Image iconImage({
  1. double? height,
  2. double? width,
  3. BoxFit? fit,
  4. Color? color,
})

This extension function is used to create an Image 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 Image.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 Image.asset widget with the specified attributes. If there is an error loading the image, it shows a placeholder with a random color.

Implementation

Image iconImage({
  double? height,
  double? width,
  BoxFit? fit,
  Color? color,
}) {
  if (this.startsWith("http")) {
    return Image.network(
      this,
      height: height ?? 24,
      width: width ?? 24,
      fit: fit ?? BoxFit.contain,
      color: color,
      errorBuilder:
          (BuildContext context, Object error, StackTrace? stackTrace) =>
              Placeholder(
        color: imagePlaceholderColor[
            Random().nextInt(imagePlaceholderColor.length)],
        fallbackHeight: height ?? 24,
        fallbackWidth: width ?? 24,
        strokeWidth: 1,
        child: SizedBox(
          height: height ?? 24,
          width: width ?? 24,
        ),
      ),
    );
  }
  return Image.asset(
    this,
    height: height ?? 24,
    width: width ?? 24,
    fit: fit ?? BoxFit.cover,
    color: color,
    errorBuilder: (context, error, stackTrace) => Placeholder(
      color: imagePlaceholderColor[
          Random().nextInt(imagePlaceholderColor.length)],
      fallbackHeight: height ?? 24,
      fallbackWidth: width ?? 24,
      strokeWidth: 1,
      child: SizedBox(
        height: height ?? 24,
        width: width ?? 24,
      ),
    ),
  );
}