I am trying to insert an image from the internet to my project, but I want it to always be cropped to the same size so that there is never an OveriSze.
I found a github post by @diegodeveloper about image cropping in Flutter where you use a . The truth is that I did not understand the code 100%, but I think I understood more or less what each line does and I tried to implement it, but this time with a . However this does not work.CustomPainter
AssetImage
NetworkImage
I understand that this function sets the package property _image
( property of the StateFulWidget where the function is located ) , but instead sets it to , causing no image to be displayed where it should be. What is the mistake?
(If I try it with a instead of a it keeps assigning )Image
dart:ui
null
AssetImage
NetworkImage
null
Future<void> _loadImage()async{
final networkImage = NetworkImage(widget.product.media[0]);
final imageKey = await networkImage.obtainKey(const ImageConfiguration());
final DecoderCallback decodeResize = (Uint8List bytes, {bool? allowUpscaling, int? cacheWidth, int? cacheHeight}) {
return ui.instantiateImageCodec(bytes,targetHeight: cacheHeight, targetWidth: cacheWidth);
};
var load = networkImage.load(imageKey, decodeResize);
ImageStreamListener listener = ImageStreamListener((info, err) async {
setState(() {
_image = info.image;
});
});
load.addListener(listener);
}
This is another attempt I made to get the ui.Image
but it also sets null:
Future<void> getImage(String path) async {
var completer = Completer<ImageInfo>();
var img = new NetworkImage(path);
img.resolve(const ImageConfiguration()).addListener(ImageStreamListener((info, _) {
completer.complete(info);
}));
ImageInfo imageInfo = await completer.future;
_image = imageInfo.image;
}
This is where I assign the value returned by the functions to the property _image
:
@override
void initState(){
// _loadImage();
// getImage(widget.product.media[0]);
super.initState();
}
The code I used seems to be outdated, here is the update:
Use:
Result