agLoadImage method

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

Loads an image dynamically based on whether it is a Network URL, Local File, or Asset.

Implementation

Widget agLoadImage({double? height, double? width, BoxFit? fit, Color? color}) {
  if (isEmpty) return _placeholderWidget(height, width);

  final sourceType = _getImageSourceType();
  if (sourceType == ImageSourceType.unknown) return _placeholderWidget(height, width);

  final isSvgImage = toLowerCase().endsWith(".svg");

  final effectiveHeight = height ?? _kDefaultImageDimension;
  final effectiveWidth = width ?? _kDefaultImageDimension;

  if (isSvgImage) {
    final colorFilter = color != null ? ColorFilter.mode(color, BlendMode.srcIn) : null;
    switch (sourceType) {
      case ImageSourceType.network:
        return SvgPicture.network(
          this,
          height: effectiveHeight,
          width: effectiveWidth,
          fit: fit ?? BoxFit.contain,
          colorFilter: colorFilter,
          placeholderBuilder: (_) => _placeholderWidget(height, width),
        );
      case ImageSourceType.localFile:
        // Ensure platform_loader handles placeholder/errors internally or returns a widget
        return platform_loader.loadFileAsSvg(
          this,
          height: effectiveHeight,
          width: effectiveWidth,
          fit: fit ?? BoxFit.contain, // Be consistent or make it a param for platform_loader
          color: color,
        );
      case ImageSourceType.asset:
        return SvgPicture.asset(
          this,
          height: effectiveHeight,
          width: effectiveWidth,
          fit: fit ?? BoxFit.cover, // Or BoxFit.contain for consistency
          colorFilter: colorFilter,
          placeholderBuilder: (_) => _placeholderWidget(height, width), // SvgPicture.asset also has placeholderBuilder
        );
      case ImageSourceType.unknown: // Already handled, but good for exhaustive switch
        return _placeholderWidget(height, width);
    }
  } else {
    // Raster image
    switch (sourceType) {
      case ImageSourceType.network:
        return Image.network(this, height: effectiveHeight, width: effectiveWidth, fit: fit ?? BoxFit.contain, color: color, errorBuilder: (_, __, ___) => _placeholderWidget(height, width));
      case ImageSourceType.localFile:
        // Ensure platform_loader handles placeholder/errors internally or returns a widget
        return platform_loader.loadFileAsImage(
          this,
          height: effectiveHeight,
          width: effectiveWidth,
          fit: fit ?? BoxFit.contain, // Be consistent
          color: color,
        );
      case ImageSourceType.asset:
        return Image.asset(
          this,
          height: effectiveHeight,
          width: effectiveWidth,
          fit: fit ?? BoxFit.cover, // Or BoxFit.contain for consistency
          color: color,
          errorBuilder: (_, __, ___) => _placeholderWidget(height, width),
        );
      case ImageSourceType.unknown:
        return _placeholderWidget(height, width);
    }
  }
}