skip to Main Content

I am viewing the camera within a restricted height area and it automatically adjusts the aspect ratio. I want to make the width equal to the device width but the view should not be distorted, I don’t want it to be a stretched view. How can I do?

Cropping is fine, all I want is to make the width equal to the screen width and the unstretched image.

I use "camera" plugin.

Widget build(BuildContext context) {
    return Container(
      height: SizeConfig.screenHeight,
      width: SizeConfig.screenWidth,
      child: Column(
        children: [
          Expanded(
            child: CameraPreview(_controller),
          ),
          SizedBox(
            height: 230,
            child: ///some elements here, this area fix
          ),
        ],
      ),
    );
  }

I tried many resizing solutions but they all resulted in a stretched view.

2

Answers


  1. Chosen as BEST ANSWER

    Inspired by Sanket Patel's suggestion, I got what I wanted as follows.

    ClipRect(
                      child: Transform.scale(
                        scale: _controller.value.aspectRatio,
                        child: Center(
                          child: CameraPreview(_controller),
                        ),
                      ),
                    )
    

  2. Try the below code where you want to show your camera.

    Modify values according to your requirement.

    return AspectRatio(
            aspectRatio: 1,
            child: ClipRect(
              child: Transform.scale(
                scale: 1 / _cameraController?.value?.aspectRatio ?? 1,
                child: Center(
                  child: AspectRatio(
                    aspectRatio: _cameraController.value.aspectRatio,
                    child: CameraPreview(_cameraController),
                  ),
                ),
              ),
            ),
          );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search