getImageWidget static method

Widget getImageWidget(
  1. String imagePath, {
  2. double? width,
  3. double? height,
  4. bool inRongCloud = true,
  5. Color? color,
  6. BoxFit fit = BoxFit.cover,
  7. String? thumbnailBase64String,
  8. bool notInAssets = false,
  9. Widget? errorWidget,
  10. Widget? placeholderWidget,
})

获取图片 Widget,可以自动判断图片路径类型

Implementation

static Widget getImageWidget(
  String imagePath, {
  double? width,
  double? height,
  bool inRongCloud = true,
  Color? color,
  BoxFit fit = BoxFit.cover,
  String? thumbnailBase64String,
  bool notInAssets = false,
  Widget? errorWidget,
  Widget? placeholderWidget,
}) {
  if (thumbnailBase64String != null && thumbnailBase64String.isNotEmpty) {
    // 只有能解码成图片的 base64 才当缩略图渲染。内容审核会把缩略图的 content 字段
    // 替换成普通文本,这类数据要按「无缩略图」处理并回退到 imagePath
    // (对齐 Android 引用消息的 thumUri -> localPath -> mediaUrl 回退顺序)。
    final Uint8List? bytes = isValidBase64Image(thumbnailBase64String)
        ? getDecodedBase64(thumbnailBase64String)
        : null;
    if (bytes != null) {
      return Image.memory(
        bytes,
        width: width,
        height: height,
        fit: fit,
        color: color,
        errorBuilder: (context, error, stackTrace) {
          return errorWidget ?? SizedBox(width: width, height: height);
        },
      );
    }
  }

  if (imagePath.isEmpty) {
    return SizedBox(width: width, height: height);
  }
  // 判断图片路径是否为网络地址
  bool isNetworkImage =
      imagePath.startsWith('http://') || imagePath.startsWith('https://');

  double circularSize = (width ?? 20) > (height ?? 20)
      ? (width ?? 20) / 2
      : (height ?? 20) / 2;

  if (isNetworkImage) {
    // 使用 CachedNetworkImage 显示网络图片
    return CachedNetworkImage(
      imageUrl: imagePath,
      width: width,
      height: height,
      fit: fit,
      placeholder: (context, url) =>
          placeholderWidget ??
          Center(
            child: SizedBox(
              width: circularSize,
              height: circularSize,
              child: CircularProgressIndicator(strokeWidth: 2),
            ),
          ), // 加载中的占位符
      errorWidget: (context, url, error) =>
          errorWidget ?? const Icon(Icons.error),
      color: color,
    );
  } else {
    // 本地资源图片,判断是否已经有 "assets/" 前缀
    if (notInAssets) {
      // 在 Web 平台上,不支持本地文件访问,显示错误图标
      if (kIsWeb) {
        return errorWidget ??
            Icon(
              Icons.error,
              size: width ?? height ?? 24,
              color: color ?? Colors.grey,
            );
      } else {
        return Image.file(
          File(imagePath),
          width: width,
          height: height,
          fit: fit,
          color: color,
          errorBuilder: (context, error, stackTrace) {
            return errorWidget ?? SizedBox(width: width, height: height);
          },
        );
      }
    } else {
      String assetPath = imagePath.startsWith('assets/')
          ? imagePath
          : 'assets/$imagePath';

      return Image.asset(
        assetPath,
        width: width,
        height: height,
        fit: fit,
        color: color,
        package: inRongCloud ? 'rongcloud_im_kit' : null,
        errorBuilder: (context, error, stackTrace) =>
            errorWidget ?? SizedBox(width: width, height: height),
      );
    }
  }
}