@@ -56,12 +56,15 @@ class CameraPickerState extends State<CameraPicker>
56
56
/// 可用的相机实例
57
57
late List <CameraDescription > cameras;
58
58
59
+ /// Whether the controller is handling taking picture or recording video.
60
+ /// 相机控制器是否在处理拍照或录像
61
+ bool isControllerBusy = false ;
62
+
59
63
/// Current exposure offset.
60
64
/// 当前曝光值
61
65
final ValueNotifier <double > currentExposureOffset = ValueNotifier <double >(0 );
62
66
final ValueNotifier <double > currentExposureSliderOffset =
63
67
ValueNotifier <double >(0 );
64
-
65
68
double maxAvailableExposureOffset = 0 ;
66
69
double minAvailableExposureOffset = 0 ;
67
70
double exposureStep = 0 ;
@@ -673,10 +676,17 @@ class CameraPickerState extends State<CameraPicker>
673
676
pickerConfig.onError,
674
677
);
675
678
}
676
- if (controller.value.isTakingPicture ) {
679
+ if (isControllerBusy ) {
677
680
return ;
678
681
}
682
+ isControllerBusy = true ;
683
+ final ExposureMode previousExposureMode = controller.value.exposureMode;
679
684
try {
685
+ await Future .wait (< Future <void >> [
686
+ controller.setFocusMode (FocusMode .locked),
687
+ if (previousExposureMode != ExposureMode .locked)
688
+ controller.setExposureMode (ExposureMode .locked),
689
+ ]);
680
690
final XFile file = await controller.takePicture ();
681
691
await controller.pausePreview ();
682
692
final bool ? isCapturedFileHandled = pickerConfig.onXFileCaptured? .call (
@@ -694,11 +704,17 @@ class CameraPickerState extends State<CameraPicker>
694
704
Navigator .of (context).pop (entity);
695
705
return ;
696
706
}
707
+ await Future .wait (< Future <void >> [
708
+ controller.setFocusMode (FocusMode .auto),
709
+ if (previousExposureMode != ExposureMode .locked)
710
+ controller.setExposureMode (previousExposureMode),
711
+ ]);
697
712
await controller.resumePreview ();
698
713
} catch (e) {
699
714
realDebugPrint ('Error when preview the captured file: $e ' );
700
715
handleErrorWithHandler (e, pickerConfig.onError);
701
716
} finally {
717
+ isControllerBusy = false ;
702
718
safeSetState (() {});
703
719
}
704
720
}
@@ -741,9 +757,10 @@ class CameraPickerState extends State<CameraPicker>
741
757
/// Set record file path and start recording.
742
758
/// 设置拍摄文件路径并开始录制视频
743
759
Future <void > startRecordingVideo () async {
744
- if (controller.value.isRecordingVideo ) {
760
+ if (isControllerBusy ) {
745
761
return ;
746
762
}
763
+ isControllerBusy = true ;
747
764
try {
748
765
await controller.startVideoRecording ();
749
766
if (isRecordingRestricted) {
@@ -756,6 +773,7 @@ class CameraPickerState extends State<CameraPicker>
756
773
..reset ()
757
774
..start ();
758
775
} catch (e, s) {
776
+ isControllerBusy = false ;
759
777
realDebugPrint ('Error when start recording video: $e ' );
760
778
if (! controller.value.isRecordingVideo) {
761
779
handleErrorWithHandler (e, pickerConfig.onError, s: s);
@@ -824,6 +842,7 @@ class CameraPickerState extends State<CameraPicker>
824
842
handleError ();
825
843
handleErrorWithHandler (e, pickerConfig.onError, s: s);
826
844
} finally {
845
+ isControllerBusy = false ;
827
846
safeSetState (() {});
828
847
}
829
848
}
@@ -973,7 +992,7 @@ class CameraPickerState extends State<CameraPicker>
973
992
/// The button to switch flash modes.
974
993
/// 切换闪光灯模式的按钮
975
994
Widget buildFlashModeSwitch (BuildContext context, CameraValue value) {
976
- IconData icon;
995
+ final IconData icon;
977
996
switch (value.flashMode) {
978
997
case FlashMode .off:
979
998
icon = Icons .flash_off;
0 commit comments