我在 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);
}
预期的是,无论我应用到相机预览的高度如何,它都不应该拉伸图像,并且应该设置适当的纵横比以显示没有拉伸的图像。有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
在BoxFit.contain
内使用Image.file
,无论高度如何,都会显示整个图像
Image.file(
_imageFile,
fit: BoxFit.contain,
)