60 lines
1.5 KiB
Dart
60 lines
1.5 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,
|
|
loadingBuilder: (context, child, loadingProgress) {
|
|
if (loadingProgress == null) {
|
|
return child;
|
|
}
|
|
// 显示占位
|
|
return Image.asset(
|
|
placeholderAsset,
|
|
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,
|
|
);
|
|
}
|
|
}
|
|
}
|