@@ -46,6 +46,7 @@ class CameraPicker extends StatefulWidget {
46
46
this .theme,
47
47
this .resolutionPreset = ResolutionPreset .max,
48
48
this .imageFormatGroup = ImageFormatGroup .unknown,
49
+ this .preferredLensDirection = CameraLensDirection .back,
49
50
this .cameraQuarterTurns = 0 ,
50
51
this .foregroundBuilder,
51
52
this .onEntitySaving,
@@ -117,6 +118,11 @@ class CameraPicker extends StatefulWidget {
117
118
/// 输出图像的格式描述
118
119
final ImageFormatGroup imageFormatGroup;
119
120
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
+
120
126
/// The foreground widget builder which will cover the whole camera preview.
121
127
/// 覆盖在相机预览上方的前景构建
122
128
final Widget Function (CameraValue )? foregroundBuilder;
@@ -145,6 +151,7 @@ class CameraPicker extends StatefulWidget {
145
151
CameraPickerTextDelegate ? textDelegate,
146
152
ResolutionPreset resolutionPreset = ResolutionPreset .max,
147
153
ImageFormatGroup imageFormatGroup = ImageFormatGroup .unknown,
154
+ CameraLensDirection preferredLensDirection = CameraLensDirection .back,
148
155
Widget Function (CameraValue )? foregroundBuilder,
149
156
EntitySaveCallback ? onEntitySaving,
150
157
CameraErrorHandler ? onError,
@@ -173,6 +180,7 @@ class CameraPicker extends StatefulWidget {
173
180
textDelegate: textDelegate,
174
181
resolutionPreset: resolutionPreset,
175
182
imageFormatGroup: imageFormatGroup,
183
+ preferredLensDirection: preferredLensDirection,
176
184
foregroundBuilder: foregroundBuilder,
177
185
onEntitySaving: onEntitySaving,
178
186
onError: onError,
@@ -485,9 +493,20 @@ class CameraPickerState extends State<CameraPicker>
485
493
);
486
494
}
487
495
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
+ }
488
507
// Initialize the controller with the given resolution preset.
489
508
_controller = CameraController (
490
- cameraDescription ?? cameras[0 ],
509
+ cameraDescription ?? cameras[index ],
491
510
widget.resolutionPreset,
492
511
enableAudio: enableAudio,
493
512
imageFormatGroup: widget.imageFormatGroup,
0 commit comments