最佳答案
理论上,您应该能够更新 CameraController
使用的 CameraValue
,以使用所需的值覆盖 previewSize,这应该会更新预览尺寸。
它不起作用。
使用以下代码片段, Controller 对象已正确更新为正确的值,但不会影响渲染。
@override
void initState() {
super.initState();
controller = CameraController(cameras[0], ResolutionPreset.max);
final size = Size(300, 300);
controller.initialize().then((_) {
controller.value = controller.value.copyWith(previewSize: size);
setState(() {});
});
}
@override
Widget build(BuildContext context) {
if (!controller.value.isInitialized) {
return Container();
}
return MaterialApp(
home: SizedBox(
height: 300,
width: 300,
child: CameraPreview(controller),
),
);
}
我什至一直在尝试在 300x300 的 SizedBox 中强制渲染布局,但这没有任何效果。
这背后的原因可能是,在幕后,camera
插件正在调用此方法来创建预览:
@override
Widget buildPreview(int cameraId) {
return Texture(textureId: cameraId);
}
关于 Texture Widget,文档是这样说的:
The size of the rectangle is determined by its parent widget, and the texture is automatically scaled to fit.
因为它不知道谁是他的 parent (我们不在小部件树中,这只是一个返回纹理小部件的方法),它占用了整个可用空间,覆盖了可以设置的任何限制实现预览的应用。
如果我的分析是正确的,那要么是一个错误,要么是为了“强制”我们进行全屏相机预览而实现的。
我仍然不明白为什么 AspectRatio
小部件对渲染没有任何影响。
更新:
我发现官方 Flutter 的 Github 上有一个问题在这里描述了你的问题。这个问题已经在 4 年前打开了:(
https://github.com/flutter/flutter/issues/15953
一些人一直在提供解决方法,最新的应该满足您的需求是 probably this one .
关于flutter - 在 Flutter 中切换相机纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72037568/