You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
3.9 KiB
148 lines
3.9 KiB
import 'dart:io'; |
|
import 'package:flutter/material.dart'; |
|
import 'package:huixiang/utils/ImgCachePath.dart'; |
|
import 'package:network_to_file_image/network_to_file_image.dart'; |
|
import 'package:path/path.dart' as p; |
|
|
|
class MImage extends StatelessWidget { |
|
final String src; |
|
final String errorSrc; |
|
final String fadeSrc; |
|
final BorderRadius radius; |
|
final double aspectRatio; |
|
final double? width; |
|
final double? height; |
|
final BoxFit fit; |
|
final bool isCircle; |
|
final bool noCompress;//不压缩图片 |
|
|
|
final double scaleIndex = 1.5; |
|
|
|
const MImage( |
|
this.src, { |
|
this.errorSrc = "assets/image/default_2_1.webp", |
|
this.fadeSrc = "assets/image/default_2_1.webp", |
|
this.aspectRatio = 0, |
|
this.width, |
|
this.height, |
|
this.fit = BoxFit.cover, |
|
this.isCircle = false, |
|
this.noCompress = false, |
|
this.radius = BorderRadius.zero, |
|
}); |
|
|
|
File fileFromDocsDir(String filename) { |
|
String pathName = p.join(ImgCachePath.instance.path, filename); |
|
return File(pathName); |
|
} |
|
|
|
double getImageWidth(context, double widgetWidth) { |
|
double width = MediaQuery.of(context).size.width; |
|
if (widgetWidth > 700) { |
|
return width; |
|
} else if (widgetWidth > 300) { |
|
return 700; |
|
} else { |
|
return 300; |
|
} |
|
} |
|
|
|
double getImageHeight(context, double widgetHeight) { |
|
if (widgetHeight > 700) { |
|
return 1000; |
|
} else if (widgetHeight > 300) { |
|
return 700; |
|
} else { |
|
return 300; |
|
} |
|
} |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
Widget image = LayoutBuilder( |
|
builder: (ctx, constraints) { |
|
String? imageUrl = ""; |
|
///压缩图片 |
|
int _w = ((getImageWidth(ctx, constraints.constrainWidth())) * scaleIndex).toInt(); |
|
int _h = ((getImageHeight(ctx, constraints.constrainHeight())) * scaleIndex).toInt(); |
|
///压缩图片 |
|
if (src.startsWith("http")) { |
|
if (noCompress) { |
|
imageUrl = src; |
|
} else { |
|
imageUrl = "$src?imageView2/0/w/${_w}/format/jpg/q/100"; |
|
} |
|
} |
|
|
|
// debugPrint("imageUrl: $imageUrl"); |
|
|
|
if (imageUrl.isEmpty) { |
|
return Image.asset( |
|
"assets/image/default_2_1.webp", |
|
fit: fit, |
|
); |
|
} |
|
Widget? cachedNetworkImage; |
|
if (src.startsWith("http")) { |
|
cachedNetworkImage = Image( |
|
image: NetworkToFileImage( |
|
url: imageUrl, |
|
file: fileFromDocsDir("resize${(noCompress?"noCompress":"$_w$_h")}${src.replaceAll("https:", "") |
|
.replaceAll("http:", "") |
|
.replaceAll("pos.upload.gznl.top", "") |
|
.replaceAll("/", "")}"), |
|
debug: true, |
|
), |
|
errorBuilder: (context, error, stackTrace) { |
|
return Image.asset( |
|
errorSrc, |
|
fit: fit, |
|
); |
|
}, |
|
fit: fit, |
|
); |
|
} else if (src.startsWith("file")) { |
|
cachedNetworkImage = Image.file( |
|
File(src), |
|
fit: fit, |
|
errorBuilder: (context, error, stackTrace) { |
|
return Image.asset( |
|
errorSrc, |
|
fit: fit, |
|
); |
|
}, |
|
); |
|
} |
|
return cachedNetworkImage ?? Image.asset( |
|
"assets/image/default_2_1.webp", |
|
fit: fit, |
|
); |
|
}, |
|
); |
|
var clipRRect; |
|
if (isCircle) { |
|
clipRRect = ClipOval(clipBehavior: Clip.antiAlias, child: image); |
|
} else { |
|
clipRRect = ClipRRect( |
|
borderRadius: radius, |
|
child: image, |
|
); |
|
} |
|
if (aspectRatio > 0) { |
|
return SizedBox( |
|
width: width, |
|
height: height, |
|
child: AspectRatio( |
|
aspectRatio: aspectRatio, |
|
child: clipRRect, |
|
), |
|
); |
|
} else { |
|
return SizedBox( |
|
width: width, |
|
height: height, |
|
child: clipRRect, |
|
); |
|
} |
|
} |
|
}
|
|
|