dreampad/lib/app/shared/utils/image_utils.dart

31 lines
912 B
Dart
Raw Normal View History

2023-11-28 10:44:58 +08:00
import 'package:cached_network_image/cached_network_image.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
class ImageUtils {
static ImageProvider getAssetImage(String name,
{ImageFormat format = ImageFormat.png}) {
return AssetImage(getImgPath(name, format: format));
}
static String getImgPath(String name,
{ImageFormat format = ImageFormat.png}) {
return 'assets/images/$name.${format.value}';
}
static ImageProvider getImageProvider(String? imageUrl,
{String holderImg = 'none'}) {
if (TextUtil.isEmpty(imageUrl)) {
return AssetImage(getImgPath(holderImg));
}
return CachedNetworkImageProvider(imageUrl!);
}
}
enum ImageFormat { png, jpg, gif, webp }
extension ImageFormatExtension on ImageFormat {
String get value => ['png', 'jpg', 'gif', 'webp'][index];
}