简体   繁体   English

如何在控制其宽度并在 Flutter 中为其指定边界半径的同时让图像扩展

[英]How to let an image extend while controlling its width and giving it a border-radius in Flutter

I am working in Flutter where I have a ListView in which I show a lot of images, I need those images to have the same width but they can extend as per their original height, ie I want a constraint on the width of the image but not the height.我在 Flutter 工作,在那里我有一个ListView ,我在其中显示了很多图像,我需要这些图像具有相同的宽度,但它们可以按照原始高度进行扩展,即我想要限制图像的宽度,但是不是高度。 It should look like the image below:它应该如下图所示:

在此处输入图片说明

Whereas the code I have written now forces every image to have the same height.而我现在编写的代码强制每个图像具有相同的高度。 Here is the code:这是代码:

                               Container(
                                  width: MediaQuery.of(context).size.width * .9,
                                  height: MediaQuery.of(context).size.height * .6,
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(35),
                                      image: DecorationImage(
                                          image: NetworkImage(memes[index].link),
                                        fit: BoxFit.fill
                                      )
                                  ),
                                ),

Thanks a lot!非常感谢!

Without height the widgets wont come into view, I suggest calculating the image height width, then building the widget according to that height from the image data would work for your problem.如果没有height ,小部件将不会出现,我建议计算图像高度宽度,然后根据图像数据的高度构建小部件将解决您的问题。 You can use this function to get the image size您可以使用此函数获取图像大小

  Future<Size> _calculateImageDimension(link) {
    Completer<Size> completer = Completer();
    Image image = Image.network(link);
    image.image.resolve(ImageConfiguration()).addListener(
      ImageStreamListener(
            (ImageInfo image, bool synchronousCall) {
          var myImage = image.image;
          Size size = Size(myImage.width.toDouble(), myImage.height.toDouble());
          completer.complete(size);
        },
      ),
    );
    return completer.future;
  }

I hope this solves your problem.我希望这能解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM