I am building a custom camera UI. I would like the CameraPreview
to be expanded so it fits the device width. This is the implementation and the result:
return Column(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: GestureDetector(
onTapUp: (details) {
focusOnTap(details);
},
onScaleStart: (details) {
zoom = _scaleFactor;
},
onScaleUpdate: (details) {
if (currentCamera == widget.normalCamera) {
onZoomChange(details.scale);
}
},
child: Stack(
children: [
CameraPreview(cameraController),
// top banner
Positioned.fill(
...
),
// bottom banner
Positioned.fill(
...
),
// zoom selector
Positioned.fill(
....
),
// top actions
SafeArea(
....
),
],
),
)),
// bottom actions
GestureDetector(
onHorizontalDragEnd: (details) {
cameraController.dispose();
widget.onSwipe(details);
},
child: Container(
....
),
),
As you can see, on the right there is a black empty space. I would like it to be gone and the camera preview expanded to match the full width.
2
Answers
Ok was easier than I thought. I was playing with
AspectRadio
andTransform.scale
, but was enough to wrapCameraPreview
in aContainer
Container(width: screenSize.width ,child: CameraPreview(cameraController))
To expand the CameraPreview to match the full width of the device, you can use the LayoutBuilder widget to get the available width and set the appropriate aspect ratio for the CameraPreview. Here’s how you can modify your code:
Here’s what’s changed:
By wrapping the CameraPreview with an AspectRatio widget and using LayoutBuilder to get the available width and height, the camera preview will now expand to match the full width of the device while maintaining its aspect ratio.