72 lines
1.6 KiB
Dart
72 lines
1.6 KiB
Dart
// Author: fengshengxiong
|
|
// Date: 2024/5/9
|
|
// Description: 加载网络图像
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:wallpaper/generated/assets.dart';
|
|
|
|
class ImageWidget extends StatelessWidget {
|
|
const ImageWidget({
|
|
super.key,
|
|
this.width,
|
|
this.height,
|
|
this.radius = 0.0,
|
|
this.url,
|
|
this.filePath,
|
|
this.isLocal = false,
|
|
this.fit = BoxFit.cover,
|
|
this.placeholder,
|
|
this.errorWidget,
|
|
});
|
|
|
|
final double? width, height;
|
|
final double radius;
|
|
final String? url;
|
|
final String? filePath;
|
|
final bool isLocal;
|
|
final BoxFit fit;
|
|
final Widget? placeholder;
|
|
final Widget? errorWidget;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ClipRRect(
|
|
borderRadius: BorderRadius.circular(radius),
|
|
child: Visibility(
|
|
visible: !isLocal,
|
|
replacement: Image.file(
|
|
File('$filePath'),
|
|
fit: fit,
|
|
width: width,
|
|
height: height,
|
|
),
|
|
child: CachedNetworkImage(
|
|
width: width,
|
|
height: height,
|
|
imageUrl: '$url',
|
|
fit: fit,
|
|
placeholder: (context, url) {
|
|
return placeholder ?? _placeErrWidget(Assets.imagesPlaceholder);
|
|
},
|
|
errorWidget: (context, url, error) {
|
|
return errorWidget ?? _placeErrWidget(Assets.imagesError);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _placeErrWidget(String imgName) {
|
|
return Container(
|
|
color: Colors.white10,
|
|
child: Image.asset(
|
|
imgName,
|
|
color: Colors.white10,
|
|
),
|
|
);
|
|
}
|
|
}
|