flutter camera - what is the difference between CameraPreview(controller) and controller.buildPreiview()

Viewed 48

I am new to flutter and I am trying to use camera with flutter.

I want to understand the difference between CameraPreview(controller) and controller.buildPreiview() because it behaves differently for some reason.

This is the code for the showing the preview:

 @override
  Widget build(BuildContext context) {
    return _isCameraInitialized
        ? Material(
            child: Stack(
                children: [
                  GestureDetector(
                    ...
                    child: _cameraController!.buildPreview()
                    // child: CameraPreview(_cameraController!)
                  ),
                  ....
                ]
            ),
        )
        : Container();

The result for using _cameraController!.buildPreview(): enter image description here

This is the desired result - make the camera preview appear as full screen.

But the result for using CameraPreview(_cameraController!) is: enter image description here

This leaves the right of the screen white and does not take the full width of the screen for some reason. I also tried to wrap it with AspectRatio but it didn't work.

I was wondering why those methods behave differently and if it is better to use one of them over the other?

1 Answers

use this solution it will remove the right vertical banner :

width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child:CameraPreview(_cameraController),
Related