2024-09-11 05:14:17 +03:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_svg/svg.dart';
|
|
|
|
|
|
|
|
class ImageUtil {
|
2025-05-28 14:52:32 +03:00
|
|
|
static Widget getImageFromPath({
|
|
|
|
required String imagePath,
|
|
|
|
double? height,
|
|
|
|
double? width,
|
|
|
|
Color? svgImageColor,
|
|
|
|
BoxFit? fit,
|
|
|
|
double? borderRadius,
|
|
|
|
}) {
|
|
|
|
final isNetwork = imagePath.startsWith('http');
|
|
|
|
final isSvg = imagePath.endsWith('.svg');
|
2024-09-11 05:14:17 +03:00
|
|
|
|
2025-05-28 14:52:32 +03:00
|
|
|
final bool ignoreSize = fit != null;
|
|
|
|
final double? _height = ignoreSize ? null : (height ?? 35);
|
|
|
|
final double? _width = ignoreSize ? null : (width ?? 35);
|
|
|
|
|
|
|
|
Widget img;
|
|
|
|
if (isNetwork) {
|
|
|
|
img = isSvg
|
|
|
|
? SvgPicture.network(imagePath,
|
2024-11-07 15:46:08 +01:00
|
|
|
key: ValueKey(imagePath),
|
2024-09-11 05:14:17 +03:00
|
|
|
height: _height,
|
|
|
|
width: _width,
|
2025-05-28 14:52:32 +03:00
|
|
|
fit: fit ?? BoxFit.contain,
|
|
|
|
placeholderBuilder: (_) => _placeholder(_height, _width))
|
|
|
|
: Image.network(imagePath,
|
2024-11-07 15:46:08 +01:00
|
|
|
key: ValueKey(imagePath),
|
2024-09-11 05:14:17 +03:00
|
|
|
height: _height,
|
|
|
|
width: _width,
|
2025-05-28 14:52:32 +03:00
|
|
|
fit: fit,
|
|
|
|
loadingBuilder: (_, child, progress) =>
|
|
|
|
progress == null ? child : _placeholder(_height, _width),
|
|
|
|
errorBuilder: (_, __, ___) => const SizedBox.shrink());
|
2024-09-11 05:14:17 +03:00
|
|
|
} else {
|
2025-05-28 14:52:32 +03:00
|
|
|
img = isSvg
|
|
|
|
? SvgPicture.asset(imagePath,
|
|
|
|
key: ValueKey(imagePath),
|
2024-10-31 02:10:40 +01:00
|
|
|
height: _height,
|
|
|
|
width: _width,
|
2025-05-28 14:52:32 +03:00
|
|
|
fit: fit ?? BoxFit.contain,
|
|
|
|
colorFilter:
|
|
|
|
svgImageColor != null ? ColorFilter.mode(svgImageColor, BlendMode.srcIn) : null,
|
|
|
|
placeholderBuilder: (_) => const Icon(Icons.error))
|
2024-10-31 02:10:40 +01:00
|
|
|
: Image.asset(
|
|
|
|
imagePath,
|
2025-05-28 14:52:32 +03:00
|
|
|
key: ValueKey(imagePath),
|
2024-10-31 02:10:40 +01:00
|
|
|
height: _height,
|
|
|
|
width: _width,
|
2025-05-28 14:52:32 +03:00
|
|
|
fit: fit,
|
|
|
|
errorBuilder: (_, __, ___) => const Icon(Icons.error),
|
2024-10-31 02:10:40 +01:00
|
|
|
);
|
2024-09-11 05:14:17 +03:00
|
|
|
}
|
2025-05-28 14:52:32 +03:00
|
|
|
|
|
|
|
if (borderRadius != null && borderRadius > 0) {
|
|
|
|
img = ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(borderRadius),
|
|
|
|
child: img,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return img;
|
2024-09-11 05:14:17 +03:00
|
|
|
}
|
2025-05-28 14:52:32 +03:00
|
|
|
|
|
|
|
static Widget _placeholder(double? h, double? w) => (h != null || w != null)
|
|
|
|
? SizedBox(height: h, width: w, child: const Center(child: CircularProgressIndicator()))
|
|
|
|
: const Center(child: CircularProgressIndicator());
|
2024-09-11 05:14:17 +03:00
|
|
|
}
|