相机预览显示拉伸图像,当高度降低时颤动

时间:2021-05-16 15:14:09

标签: flutter camera aspect-ratio

我在 flutter 中使用相机预览,我在其中为相机预览容器设置了一些高度和宽度。但是当我降低容器的高度时,图像会被拉伸。

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);
}

按照上面的代码设置高度和宽度,结果是: The black pad in the image gets stretched.

预期的是,无论我应用到相机预览的高度如何,它都不应该拉伸图像,并且应该设置适当的纵横比以显示没有拉伸的图像。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

BoxFit.contain内使用Image.file,无论高度如何,都会显示整个图像

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