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.

95 lines
2.4 KiB

4 years ago
import 'package:cached_network_image/cached_network_image.dart';
4 years ago
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
4 years ago
import 'package:flutter/material.dart';
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;
4 years ago
final bool isCircle;
4 years ago
3 years ago
double scaleIndex = 2.5;
3 years ago
MImage(
this.src, {
4 years ago
this.errorSrc = "assets/image/default_2_1.png",
this.fadeSrc = "assets/image/default_2_1.png",
4 years ago
this.aspectRatio,
this.width,
this.height,
this.fit,
4 years ago
this.isCircle = false,
4 years ago
this.radius = BorderRadius.zero,
});
@override
Widget build(BuildContext context) {
4 years ago
Widget image = LayoutBuilder(builder: (context, constraints) {
3 years ago
String imageUrl = "";
if (src != null && src != "") {
3 years ago
imageUrl = "$src?imageMogr2/thumbnail/${constraints.constrainWidth() * scaleIndex}"
"x${constraints.constrainHeight() * scaleIndex}/format/webp/blur/1x0/quality/75";
3 years ago
}
3 years ago
if (imageUrl == null || imageUrl == "") {
return Image.asset(
"assets/image/default_2_1.png",
fit: fit,
);
}
4 years ago
return CachedNetworkImage(
imageUrl: imageUrl,
cacheManager: DefaultCacheManager(),
fadeInDuration: Duration(milliseconds: 300),
fadeOutDuration: Duration(milliseconds: 300),
imageBuilder: (context, provide) {
return Image(
image: provide,
fit: fit,
);
},
errorWidget: (context, error, stackTrace) {
return Image.asset(
errorSrc,
fit: fit,
);
},
placeholder: (context, placeholder) {
return Image.asset(
fadeSrc,
fit: fit,
);
},
);
});
3 years ago
var clipRRect;
4 years ago
if (isCircle) {
3 years ago
clipRRect = ClipOval(clipBehavior: Clip.antiAlias, child: image);
4 years ago
} else {
clipRRect = ClipRRect(
borderRadius: radius,
4 years ago
child: image,
4 years ago
);
}
4 years ago
if (aspectRatio != null && aspectRatio > 0) {
return Container(
child: AspectRatio(
aspectRatio: aspectRatio,
child: clipRRect,
),
);
} else {
return Container(
width: width,
height: height,
child: clipRRect,
);
}
}
}