imageWidgetOf function

Widget? imageWidgetOf(
  1. BuildContext context,
  2. dynamic imageSource, {
  3. Key? key,
  4. bool circle = true,
  5. double size = kDefaultImageSize,
  6. Widget? placeholder,
  7. Color? background,
  8. bool fallbackToPlaceholder = true,
  9. dynamic iconOverlay,
})

Loads an image widget from a variety of known sources: Uri, String, File, bytes. The placeholder is only used when the imageSource is a network source (Uri, String). In that case, the placeholder is displayed while the image is loading.

Implementation

Widget? imageWidgetOf(
  BuildContext context,
  final imageSource, {
  Key? key,
  bool circle = true,
  double size = kDefaultImageSize,
  Widget? placeholder,
  Color? background,
  bool fallbackToPlaceholder = true,
  iconOverlay,
}) {
  if (imageSource is Iterable) {
    final Iterable allOptions = [
      ...imageSource,
      if (fallbackToPlaceholder && placeholder != null) placeholder,
    ].where((_) => _ != null);
    final eachImageOption = allOptions.map((source) {
      return imageWidgetOf(
        context,
        source,
        circle: circle,
        key: key,
        size: size,
        placeholder: placeholder,
        fallbackToPlaceholder: false,
        iconOverlay: iconOverlay,
      );
    });
    return eachImageOption.firstOrNull;
  }

  Widget? widget;

  if (imageSource is IconInfo) {
    if (imageSource.type == IconType.named_) {
      widget = context
          .findIcon(imageSource.icon)
          ?.copyWith(
            size: size,
            color: imageSource.background.toColor(),
            circular: imageSource.circular,
          )
          .widget;
      if (widget == null) {
        _log.severe(
            "Missing named icon ${imageSource.icon}", null, StackTrace.current);
      }
    }
    background ??= imageSource.backgroundColor;
  } else if (imageSource is NamedIcon) {
    widget = imageSource
        .copyWith(size: size, circular: circle, color: background)
        .icon;
  } else if (imageSource is IconData) {
    widget = Icon(imageSource, size: size, color: background);
  } else if (imageSource is Icon) {
    widget = Icon(imageSource.icon, size: size, color: background);
  }
  if (widget is Icon && circle != true) {
    /// Let it pass through
  } else if (imageSource is Widget) {
    widget = _widgetImage(imageSource,
        key: key, circle: circle, size: size, background: background);
  } else if (widget == null) {
    final ImageProvider? data = imageProviderOf(imageSource);
    final fallback = fallbackToPlaceholder ? placeholder : null;
    if (data == null && fallback == null) return null;
    if (data is CachedNetworkImageProvider) {
      widget = _networkImage(data, placeholder,
          key: key, background: background, circle: circle, size: size);
    } else {
      widget = _memoryImage(data,
          key: key,
          fallback: fallback,
          background: background,
          circle: circle,
          size: size);
    }
  }

  if (iconOverlay != null) {
    // Figure out appropriate offset padding - we have to make the containing box slightly larger to accommodate for
    // the overlay icon needing to extend further (to not overlay as much)
    final padding = EdgeInsets.only(
        bottom: (size ~/ 15).roundToDouble(),
        right: (size ~/ 15).roundToDouble());
    return Stack(
      alignment: AlignmentDirectional.bottomEnd,
      children: [
        Container(
          padding: padding,
          child: widget,
        ),
        if (iconOverlay is NamedIcon) iconOverlay.build(size: size / 3),
        if (iconOverlay is! NamedIcon) iconOverlay as Widget,
      ],
    );
  } else {
    return widget;
  }
}