Skip to content

Commit 0400d4e

Browse files
authored
preferredLensDirection (#57)
1 parent d79b23a commit 0400d4e

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/src/widgets/camera_picker.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class CameraPicker extends StatefulWidget {
4646
this.theme,
4747
this.resolutionPreset = ResolutionPreset.max,
4848
this.imageFormatGroup = ImageFormatGroup.unknown,
49+
this.preferredLensDirection = CameraLensDirection.back,
4950
this.cameraQuarterTurns = 0,
5051
this.foregroundBuilder,
5152
this.onEntitySaving,
@@ -117,6 +118,11 @@ class CameraPicker extends StatefulWidget {
117118
/// 输出图像的格式描述
118119
final ImageFormatGroup imageFormatGroup;
119120

121+
/// Which lens direction is preferred when first using the camera,
122+
/// typically with the front or the back direction.
123+
/// 首次使用相机时首选的镜头方向,通常是前置或后置。
124+
final CameraLensDirection preferredLensDirection;
125+
120126
/// The foreground widget builder which will cover the whole camera preview.
121127
/// 覆盖在相机预览上方的前景构建
122128
final Widget Function(CameraValue)? foregroundBuilder;
@@ -145,6 +151,7 @@ class CameraPicker extends StatefulWidget {
145151
CameraPickerTextDelegate? textDelegate,
146152
ResolutionPreset resolutionPreset = ResolutionPreset.max,
147153
ImageFormatGroup imageFormatGroup = ImageFormatGroup.unknown,
154+
CameraLensDirection preferredLensDirection = CameraLensDirection.back,
148155
Widget Function(CameraValue)? foregroundBuilder,
149156
EntitySaveCallback? onEntitySaving,
150157
CameraErrorHandler? onError,
@@ -173,6 +180,7 @@ class CameraPicker extends StatefulWidget {
173180
textDelegate: textDelegate,
174181
resolutionPreset: resolutionPreset,
175182
imageFormatGroup: imageFormatGroup,
183+
preferredLensDirection: preferredLensDirection,
176184
foregroundBuilder: foregroundBuilder,
177185
onEntitySaving: onEntitySaving,
178186
onError: onError,
@@ -485,9 +493,20 @@ class CameraPickerState extends State<CameraPicker>
485493
);
486494
}
487495

496+
final int preferredIndex = cameras.indexWhere(
497+
(CameraDescription e) =>
498+
e.lensDirection == widget.preferredLensDirection,
499+
);
500+
final int index;
501+
if (preferredIndex != -1 && _c == null) {
502+
index = preferredIndex;
503+
currentCameraIndex = preferredIndex;
504+
} else {
505+
index = currentCameraIndex;
506+
}
488507
// Initialize the controller with the given resolution preset.
489508
_controller = CameraController(
490-
cameraDescription ?? cameras[0],
509+
cameraDescription ?? cameras[index],
491510
widget.resolutionPreset,
492511
enableAudio: enableAudio,
493512
imageFormatGroup: widget.imageFormatGroup,

0 commit comments

Comments
 (0)