Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[camera] - Fix component CameraPreview on Flutter use impeller #8068

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/camera/camera/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# FVM Version Cache
.fvm/
.fvmrc
66 changes: 37 additions & 29 deletions packages/camera/camera/lib/src/camera_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io';
import 'dart:math' as math;

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand All @@ -21,26 +24,38 @@ class CameraPreview extends StatelessWidget {

@override
Widget build(BuildContext context) {
return controller.value.isInitialized
? ValueListenableBuilder<CameraValue>(
valueListenable: controller,
builder: (BuildContext context, Object? value, Widget? child) {
return AspectRatio(
aspectRatio: _isLandscape()
? controller.value.aspectRatio
: (1 / controller.value.aspectRatio),
child: Stack(
fit: StackFit.expand,
children: <Widget>[
_wrapInRotatedBox(child: controller.buildPreview()),
child ?? Container(),
],
),
);
},
child: child,
)
: Container();
return controller.value.isInitialized ? _getBuildPreviewComponent() : Container();
}

Widget _getBuildPreviewComponent() {
if (Platform.isAndroid || Platform.isIOS) {
return Transform(
alignment: Alignment.center,
transform: Matrix4.rotationY(math.pi),
child: _buildCameraPreview(),
);
}

return _buildCameraPreview();
}

Widget _buildCameraPreview() {
return ValueListenableBuilder<CameraValue>(
valueListenable: controller,
builder: (BuildContext context, Object? value, Widget? child) {
return AspectRatio(
aspectRatio: _isLandscape() ? controller.value.aspectRatio : (1 / controller.value.aspectRatio),
child: Stack(
fit: StackFit.expand,
children: <Widget>[
_wrapInRotatedBox(child: controller.buildPreview()),
child ?? Container(),
],
),
);
},
child: child,
);
}

Widget _wrapInRotatedBox({required Widget child}) {
Expand All @@ -55,10 +70,7 @@ class CameraPreview extends StatelessWidget {
}

bool _isLandscape() {
return <DeviceOrientation>[
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight
].contains(_getApplicableOrientation());
return <DeviceOrientation>[DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight].contains(_getApplicableOrientation());
}

int _getQuarterTurns() {
Expand All @@ -72,10 +84,6 @@ class CameraPreview extends StatelessWidget {
}

DeviceOrientation _getApplicableOrientation() {
return controller.value.isRecordingVideo
? controller.value.recordingOrientation!
: (controller.value.previewPauseOrientation ??
controller.value.lockedCaptureOrientation ??
controller.value.deviceOrientation);
return controller.value.isRecordingVideo ? controller.value.recordingOrientation! : (controller.value.previewPauseOrientation ?? controller.value.lockedCaptureOrientation ?? controller.value.deviceOrientation);
}
}