skip to Main Content

I have added a camera/video function to my app, but the preview is not matching the screen size. I tried few solutions, but in most of the cases it’s not changing anything or the ratio is off.

Below is the code:

Widget build(BuildContext context) {
    final mediaSize = MediaQuery.of(context).size;

    return Scaffold(
      extendBody: true,
      extendBodyBehindAppBar: true,
      appBar: AppBar(
          backgroundColor: Colors.transparent,
          elevation: 0,
          leading: Builder(builder: (BuildContext context) {
            return IconButton(
                onPressed: () {
                  Navigator.pop(context);
                },
                icon: const Icon(UniconsLine.multiply,
                    color: Colors.white, size: 30));
          })),
      body: FutureBuilder<void>(
        future: _initializeControllerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            // If the Future is complete, display the preview.
            return Transform.scale(
              scale: 1 /
                  (_cameraController!.value.aspectRatio *
                      mediaSize.aspectRatio),
              alignment: Alignment.center,
              child: CameraPreview(_cameraController!),
            );
          } else {
            // Otherwise, display a loading indicator.
            return const Center(child: CircularProgressIndicator());
          }
        },
      ),
      floatingActionButton: FloatingActionButton(
        // Provide an onPressed callback.
        onPressed: () async {
          // Take the Picture in a try / catch block. If anything goes wrong,
          // catch the error.
          try {
            // Ensure that the camera is initialized.
            await _initializeControllerFuture;

            // Attempt to take a picture and get the file `image`
            // where it was saved.
            final image = await _cameraController?.takePicture();

            if (!mounted) return;

            // If the picture was taken, display it on a new screen.
            await Navigator.of(context).push(
              MaterialPageRoute(
                builder: (context) => DisplayPictureScreen(
                  // Pass the automatically generated path to
                  // the DisplayPictureScreen widget.
                  imagePath: image!.path,
                ),
              ),
            );
          } catch (e) {
            // If an error occurs, log the error to the console.
            print(e);
          }
        },
        child: (widget.isVideo)
            ? const Icon(UniconsLine.video)
            : const Icon(UniconsLine.camera),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }

Any idea ?
Thanks

2

Answers


  1. 你可以试一试这个第三方库—-flutter_screenutil,按照设计图的比例填写你需要的大小,它会自动适配你的屏幕

    Login or Signup to reply.
  2. Here is an answer that should work

    To fix the issue with the camera preview not matching the screen size, you can try the following modifications to the code:

    1. Replace the Transform.scale widget with a AspectRatio widget to ensure that the camera preview maintains the correct aspect ratio:
    AspectRatio(
      aspectRatio: _cameraController!.value.aspectRatio,
      child: CameraPreview(_cameraController!),
    ),
    
    1. Update the scale property in the Transform.scale widget to adjust the preview size correctly:
    scale: mediaSize.aspectRatio / _cameraController!.value.aspectRatio,
    

    Here’s the modified code:

    Widget build(BuildContext context) {
      final mediaSize = MediaQuery.of(context).size;
    
      return Scaffold(
        extendBody: true,
        extendBodyBehindAppBar: true,
        appBar: AppBar(
          backgroundColor: Colors.transparent,
          elevation: 0,
          leading: Builder(
            builder: (BuildContext context) {
              return IconButton(
                onPressed: () {
                  Navigator.pop(context);
                },
                icon: const Icon(
                  UniconsLine.multiply,
                  color: Colors.white,
                  size: 30,
                ),
              );
            },
          ),
        ),
        body: FutureBuilder<void>(
          future: _initializeControllerFuture,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              // If the Future is complete, display the preview.
              return AspectRatio(
                aspectRatio: _cameraController!.value.aspectRatio,
                child: CameraPreview(_cameraController!),
              );
            } else {
              // Otherwise, display a loading indicator.
              return const Center(child: CircularProgressIndicator());
            }
          },
        ),
        floatingActionButton: FloatingActionButton(
          // Provide an onPressed callback.
          onPressed: () async {
            // Take the Picture in a try / catch block. If anything goes wrong,
            // catch the error.
            try {
              // Ensure that the camera is initialized.
              await _initializeControllerFuture;
    
              // Attempt to take a picture and get the file `image`
              // where it was saved.
              final image = await _cameraController?.takePicture();
    
              if (!mounted) return;
    
              // If the picture was taken, display it on a new screen.
              await Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => DisplayPictureScreen(
                    // Pass the automatically generated path to
                    // the DisplayPictureScreen widget.
                    imagePath: image!.path,
                  ),
                ),
              );
            } catch (e) {
              // If an error occurs, log the error to the console.
              print(e);
            }
          },
          child: (widget.isVideo)
              ? const Icon(UniconsLine.video)
              : const Icon(UniconsLine.camera),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      );
    }
    

    With these changes, the camera preview should match the screen size correctly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search