2025-09-17 15:32:18 +08:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2025-08-21 10:50:38 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
class NetworkOrAssetImage extends StatelessWidget {
|
|
|
|
final String? imageUrl;
|
|
|
|
final double width;
|
|
|
|
final double? height;
|
|
|
|
final BoxFit fit;
|
|
|
|
final String placeholderAsset;
|
|
|
|
|
|
|
|
const NetworkOrAssetImage({
|
|
|
|
super.key,
|
|
|
|
required this.imageUrl,
|
|
|
|
this.width = 60.0,
|
|
|
|
this.height,
|
|
|
|
this.fit = BoxFit.cover,
|
|
|
|
this.placeholderAsset = 'assets/images/avatar/default.png',
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final isNetwork = imageUrl != null && imageUrl!.isNotEmpty && (imageUrl!.startsWith('http://') || imageUrl!.startsWith('https://'));
|
|
|
|
|
|
|
|
if (isNetwork) {
|
2025-09-17 15:32:18 +08:00
|
|
|
return CachedNetworkImage(
|
|
|
|
imageUrl: imageUrl!,
|
2025-08-21 10:50:38 +08:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
fit: fit,
|
2025-09-17 15:32:18 +08:00
|
|
|
placeholder: (context, url) => Image.asset(
|
|
|
|
placeholderAsset.isEmpty ? 'assets/images/avatar/default.png' : placeholderAsset,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
fit: fit,
|
|
|
|
),
|
|
|
|
errorWidget: (context, url, error) => Image.asset(
|
|
|
|
placeholderAsset,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
fit: fit,
|
|
|
|
),
|
2025-08-21 10:50:38 +08:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Image.asset(
|
|
|
|
(imageUrl != null && imageUrl!.isNotEmpty) ? imageUrl! : placeholderAsset,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
fit: fit,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|