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
你可以试一试这个第三方库—-flutter_screenutil,按照设计图的比例填写你需要的大小,它会自动适配你的屏幕
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:
Transform.scale
widget with aAspectRatio
widget to ensure that the camera preview maintains the correct aspect ratio:scale
property in theTransform.scale
widget to adjust the preview size correctly:Here’s the modified code:
With these changes, the camera preview should match the screen size correctly.