flutter/lib/components/network_or_asset_image.dart
2025-08-21 10:50:38 +08:00

48 lines
1.2 KiB
Dart

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) {
return Image.network(
imageUrl!,
width: width,
height: height,
fit: fit,
errorBuilder: (context, error, stackTrace) {
return Image.asset(
placeholderAsset,
width: width,
height: height,
fit: fit,
);
},
);
} else {
return Image.asset(
(imageUrl != null && imageUrl!.isNotEmpty) ? imageUrl! : placeholderAsset,
width: width,
height: height,
fit: fit,
);
}
}
}