|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
class MImage extends StatelessWidget {
|
|
|
|
final String src;
|
|
|
|
final String errorSrc;
|
|
|
|
final String fadeSrc;
|
|
|
|
final BorderRadius radius;
|
|
|
|
final double aspectRatio;
|
|
|
|
double width;
|
|
|
|
double height;
|
|
|
|
final BoxFit fit;
|
|
|
|
final bool isCircle;
|
|
|
|
|
|
|
|
double scaleIndex = 2.5;
|
|
|
|
|
|
|
|
MImage(
|
|
|
|
this.src, {
|
|
|
|
this.errorSrc = "assets/image/default_2_1.png",
|
|
|
|
this.fadeSrc = "assets/image/default_2_1.png",
|
|
|
|
this.aspectRatio,
|
|
|
|
this.width,
|
|
|
|
this.height,
|
|
|
|
this.fit,
|
|
|
|
this.isCircle = false,
|
|
|
|
this.radius = BorderRadius.zero,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
Widget image = LayoutBuilder(
|
|
|
|
builder: (context, constraints) {
|
|
|
|
String imageUrl = "";
|
|
|
|
if (src != null && src != "" && src.startsWith("http")) {
|
|
|
|
imageUrl =
|
|
|
|
"$src?imageMogr2/thumbnail/${constraints.constrainWidth() * scaleIndex}"
|
|
|
|
"x${constraints.constrainHeight() * scaleIndex}/format/webp/quality/100";
|
|
|
|
}
|
|
|
|
|
|
|
|
// print("constrainWidth: ${constraints.constrainWidth()}");
|
|
|
|
// print("constrainHeight: ${constraints.constrainHeight()}");
|
|
|
|
|
|
|
|
if (imageUrl == null || imageUrl == "") {
|
|
|
|
return Image.asset(
|
|
|
|
"assets/image/default_2_1.png",
|
|
|
|
fit: fit,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Widget cachedNetworkImage;
|
|
|
|
if (src.startsWith("http")) {
|
|
|
|
cachedNetworkImage = 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,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else if (src.startsWith("file")) {
|
|
|
|
cachedNetworkImage = Image.file(
|
|
|
|
File(src),
|
|
|
|
fit: fit,
|
|
|
|
errorBuilder: (context, error, stackTrace) {
|
|
|
|
return Image.asset(
|
|
|
|
errorSrc,
|
|
|
|
fit: fit,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return cachedNetworkImage;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
var clipRRect;
|
|
|
|
if (isCircle) {
|
|
|
|
clipRRect = ClipOval(clipBehavior: Clip.antiAlias, child: image);
|
|
|
|
} else {
|
|
|
|
clipRRect = ClipRRect(
|
|
|
|
borderRadius: radius,
|
|
|
|
child: image,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (aspectRatio != null && aspectRatio > 0) {
|
|
|
|
return Container(
|
|
|
|
child: AspectRatio(
|
|
|
|
aspectRatio: aspectRatio,
|
|
|
|
child: clipRRect,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Container(
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
child: clipRRect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|