简体   繁体   English

相机预览显示拉伸图像,当 flutter 中的高度减小时

[英]Camera preview shows stretched image, when height decreases in flutter

I am using camera preview in flutter, where I am setting some height and width to camera preview container.我在 flutter 中使用相机预览,我在其中为相机预览容器设置了一些高度和宽度。 But when I decrease the height of container, the image gets stretched.但是当我降低容器的高度时,图像会被拉伸。

Container(
  height: MediaQuery.of(context).size.height * 0.35,
  width: MediaQuery.of(context).size.width * 0.90,
  child: _cameraPreviewWidget(),
)

/// Display Camera preview.
Widget _cameraPreviewWidget() {
  if (controller == null || !controller.value.isInitialized) {
    return const Text(
      'Loading',
      style: TextStyle(
        color: Colors.white,
        fontSize: 20.0,
        fontWeight: FontWeight.w900,
      ),
    );
  }
  return CameraPreview(controller);
}

Setting the height and width as per shown in above code, and the result is:按照上面的代码设置高度和宽度,结果是: 图像中的黑色垫被拉伸。

Expected is, whatever height I apply to the camera preview, it should not stretch the image, and should set appropriate aspect ratio to show image without stretch.预期的是,无论我应用于相机预览的高度如何,它都不应该拉伸图像,并且应该设置适当的纵横比以显示不拉伸的图像。 Can someone please help me in this issue?有人可以帮我解决这个问题吗?

Use BoxFit.contain inside Image.file , it will show the entire image irrespective of heightImage.file中使用BoxFit.contain ,它将显示整个图像,而与高度无关

   Image.file(
             _imageFile,
             fit: BoxFit.contain,
            )

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

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