2024-09-11 02:14:17 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_svg/svg.dart';
|
|
|
|
|
|
|
|
class ImageUtil {
|
|
|
|
static Widget getImageFromPath({required String imagePath, double? height, double? width}) {
|
|
|
|
final bool isNetworkImage = imagePath.startsWith('http') || imagePath.startsWith('https');
|
|
|
|
final bool isSvg = imagePath.endsWith('.svg');
|
|
|
|
final double _height = height ?? 35;
|
|
|
|
final double _width = width ?? 35;
|
|
|
|
|
|
|
|
if (isNetworkImage) {
|
|
|
|
return isSvg
|
|
|
|
? SvgPicture.network(
|
2024-11-07 14:46:08 +00:00
|
|
|
key: ValueKey(imagePath),
|
2024-09-11 02:14:17 +00:00
|
|
|
imagePath,
|
|
|
|
height: _height,
|
|
|
|
width: _width,
|
|
|
|
placeholderBuilder: (BuildContext context) => Container(
|
|
|
|
height: _height,
|
|
|
|
width: _width,
|
|
|
|
child: Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: Image.network(
|
2024-11-07 14:46:08 +00:00
|
|
|
key: ValueKey(imagePath),
|
2024-09-11 02:14:17 +00:00
|
|
|
imagePath,
|
|
|
|
height: _height,
|
|
|
|
width: _width,
|
|
|
|
loadingBuilder:
|
|
|
|
(BuildContext context, Widget child, ImageChunkEvent? loadingProgress) {
|
|
|
|
if (loadingProgress == null) {
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
return Container(
|
|
|
|
height: _height,
|
|
|
|
width: _width,
|
|
|
|
child: Center(
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
value: loadingProgress.expectedTotalBytes != null
|
|
|
|
? loadingProgress.cumulativeBytesLoaded /
|
|
|
|
loadingProgress.expectedTotalBytes!
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
|
|
|
|
return Container(
|
|
|
|
height: _height,
|
|
|
|
width: _width,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return isSvg
|
2024-10-31 01:10:40 +00:00
|
|
|
? SvgPicture.asset(
|
|
|
|
imagePath,
|
|
|
|
height: _height,
|
|
|
|
width: _width,
|
|
|
|
placeholderBuilder: (_) => Icon(Icons.error),
|
2024-11-07 14:46:08 +00:00
|
|
|
key: ValueKey(imagePath),
|
2024-10-31 01:10:40 +00:00
|
|
|
)
|
|
|
|
: Image.asset(
|
|
|
|
imagePath,
|
|
|
|
height: _height,
|
|
|
|
width: _width,
|
|
|
|
errorBuilder: (_, __, ___) => Icon(Icons.error),
|
2024-11-07 14:46:08 +00:00
|
|
|
key: ValueKey(imagePath),
|
2024-10-31 01:10:40 +00:00
|
|
|
);
|
2024-09-11 02:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|