Skip to content

Commit

Permalink
add check platform to build cameraPreview component
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonathanqz committed Nov 13, 2024
1 parent f6c9214 commit eadef58
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions packages/camera/camera/lib/src/camera_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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';
Expand All @@ -23,27 +24,38 @@ class CameraPreview extends StatelessWidget {

@override
Widget build(BuildContext context) {
return controller.value.isInitialized
? Transform(
alignment: Alignment.center,
transform: Matrix4.rotationY(math.pi),
child: 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 Down

0 comments on commit eadef58

Please sign in to comment.