Skip to content

Commit 817c9b2

Browse files
committed
🎨 Styles improvement
1 parent f536fee commit 817c9b2

File tree

3 files changed

+94
-82
lines changed

3 files changed

+94
-82
lines changed

example/lib/main.dart

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,24 @@ class _MyHomePageState extends State<MyHomePage> {
4747
Future<void> pick(BuildContext context) async {
4848
final Size size = MediaQuery.of(context).size;
4949
final double scale = MediaQuery.of(context).devicePixelRatio;
50-
try {
51-
final AssetEntity? result = await CameraPicker.pickFromCamera(
52-
context,
53-
pickerConfig: const CameraPickerConfig(enableRecording: true),
50+
final AssetEntity? result = await CameraPicker.pickFromCamera(
51+
context,
52+
pickerConfig: const CameraPickerConfig(enableRecording: true),
53+
);
54+
if (result != null && result != entity) {
55+
entity = result;
56+
if (mounted) {
57+
setState(() {});
58+
}
59+
data = await result.thumbnailDataWithSize(
60+
ThumbnailSize(
61+
(size.width * scale).toInt(),
62+
(size.height * scale).toInt(),
63+
),
5464
);
55-
if (result != null && result != entity) {
56-
entity = result;
57-
if (mounted) {
58-
setState(() {});
59-
}
60-
data = await result.thumbnailDataWithSize(
61-
ThumbnailSize(
62-
(size.width * scale).toInt(),
63-
(size.height * scale).toInt(),
64-
),
65-
);
66-
if (mounted) {
67-
setState(() {});
68-
}
65+
if (mounted) {
66+
setState(() {});
6967
}
70-
} catch (e) {
71-
rethrow;
7268
}
7369
}
7470

lib/src/constants/type_defs.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ typedef CameraErrorHandler = void Function(
4444
/// Build the foreground/overlay widget with the given [CameraValue].
4545
/// 根据给定的 [CameraValue] 构建自定义的前景 widget
4646
///
47-
/// The [cameraValue] will be null until the [CameraController] initialized.
48-
/// 在 [CameraController] 完成初始化前,[cameraValue] 将为空。
47+
/// The `cameraValue` will be null until the [CameraController] initialized.
48+
/// 在 [CameraController] 完成初始化前,`cameraValue` 将为空。
4949
/// {@endtemplate}
5050
typedef ForegroundBuilder = Widget Function(
5151
BuildContext context,

lib/src/widgets/camera_picker.dart

Lines changed: 76 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -418,18 +418,26 @@ class CameraPickerState extends State<CameraPicker>
418418
await newController.prepareForVideoRecording();
419419
}
420420
// Then call other asynchronous methods.
421-
await Future.wait(<Future<void>>[
422-
if (config.lockCaptureOrientation != null)
423-
newController.lockCaptureOrientation(config.lockCaptureOrientation),
424-
(() async => _maxAvailableExposureOffset =
425-
await newController.getMaxExposureOffset())(),
426-
(() async => _minAvailableExposureOffset =
427-
await newController.getMinExposureOffset())(),
428-
(() async =>
429-
_maxAvailableZoom = await newController.getMaxZoomLevel())(),
430-
(() async =>
431-
_minAvailableZoom = await newController.getMinZoomLevel())(),
432-
]);
421+
await Future.wait(
422+
<Future<void>>[
423+
if (config.lockCaptureOrientation != null)
424+
newController
425+
.lockCaptureOrientation(config.lockCaptureOrientation),
426+
newController
427+
.getMinExposureOffset()
428+
.then((double value) => _maxAvailableExposureOffset = value),
429+
newController
430+
.getMaxExposureOffset()
431+
.then((double value) => _minAvailableExposureOffset = value),
432+
newController
433+
.getMaxZoomLevel()
434+
.then((double value) => _maxAvailableZoom = value),
435+
newController
436+
.getMinZoomLevel()
437+
.then((double value) => _minAvailableZoom = value),
438+
],
439+
eagerError: true,
440+
);
433441
_controller = newController;
434442
} catch (e, s) {
435443
handleErrorWithHandler(e, config.onError, s: s);
@@ -700,26 +708,34 @@ class CameraPickerState extends State<CameraPicker>
700708
/// Set record file path and start recording.
701709
/// 设置拍摄文件路径并开始录制视频
702710
Future<void> startRecordingVideo() async {
703-
if (!controller.value.isRecordingVideo) {
704-
controller.startVideoRecording().then((dynamic _) {
705-
safeSetState(() {});
706-
if (isRecordingRestricted) {
707-
_recordCountdownTimer = Timer(maximumRecordingDuration!, () {
708-
stopRecordingVideo();
709-
});
710-
}
711-
}).catchError((Object e) {
712-
realDebugPrint('Error when start recording video: $e');
713-
if (controller.value.isRecordingVideo) {
714-
controller.stopVideoRecording().catchError((Object e) {
715-
realDebugPrint(
716-
'Error when stop recording video after an error start: $e',
717-
);
718-
stopRecordingVideo();
719-
});
720-
}
721-
handleErrorWithHandler(e, config.onError);
722-
});
711+
if (controller.value.isRecordingVideo) {
712+
return;
713+
}
714+
try {
715+
await controller.startVideoRecording();
716+
if (isRecordingRestricted) {
717+
_recordCountdownTimer = Timer(maximumRecordingDuration!, () {
718+
stopRecordingVideo();
719+
});
720+
}
721+
} catch (e, s) {
722+
realDebugPrint('Error when start recording video: $e');
723+
if (!controller.value.isRecordingVideo) {
724+
handleErrorWithHandler(e, config.onError, s: s);
725+
return;
726+
}
727+
try {
728+
await controller.stopVideoRecording();
729+
} catch (e, s) {
730+
realDebugPrint(
731+
'Error when stop recording video after an error start: $e',
732+
);
733+
_recordCountdownTimer?.cancel();
734+
isShootingButtonAnimate = false;
735+
handleErrorWithHandler(e, config.onError, s: s);
736+
}
737+
} finally {
738+
safeSetState(() {});
723739
}
724740
}
725741

@@ -732,36 +748,36 @@ class CameraPickerState extends State<CameraPicker>
732748
safeSetState(() {});
733749
}
734750

735-
if (controller.value.isRecordingVideo) {
736-
try {
737-
final XFile file = await controller.stopVideoRecording();
738-
final bool? isCapturedFileHandled = config.onXFileCaptured?.call(
739-
file,
740-
CameraPickerViewType.video,
741-
);
742-
if (isCapturedFileHandled ?? false) {
743-
return;
744-
}
745-
final AssetEntity? entity = await _pushToViewer(
746-
file: file,
747-
viewType: CameraPickerViewType.video,
748-
);
749-
if (entity != null) {
750-
Navigator.of(context).pop(entity);
751-
}
752-
} catch (e) {
753-
realDebugPrint('Error when stop recording video: $e');
754-
realDebugPrint('Try to initialize a new CameraController...');
755-
initCameras();
756-
_handleError();
757-
handleErrorWithHandler(e, config.onError);
758-
} finally {
759-
isShootingButtonAnimate = false;
760-
safeSetState(() {});
761-
}
751+
if (!controller.value.isRecordingVideo) {
752+
_handleError();
762753
return;
763754
}
764-
_handleError();
755+
try {
756+
final XFile file = await controller.stopVideoRecording();
757+
final bool? isCapturedFileHandled = config.onXFileCaptured?.call(
758+
file,
759+
CameraPickerViewType.video,
760+
);
761+
if (isCapturedFileHandled ?? false) {
762+
return;
763+
}
764+
final AssetEntity? entity = await _pushToViewer(
765+
file: file,
766+
viewType: CameraPickerViewType.video,
767+
);
768+
if (entity != null) {
769+
Navigator.of(context).pop(entity);
770+
}
771+
} catch (e, s) {
772+
realDebugPrint('Error when stop recording video: $e');
773+
realDebugPrint('Try to initialize a new CameraController...');
774+
initCameras();
775+
_handleError();
776+
handleErrorWithHandler(e, config.onError, s: s);
777+
} finally {
778+
isShootingButtonAnimate = false;
779+
safeSetState(() {});
780+
}
765781
}
766782

767783
Future<AssetEntity?> _pushToViewer({

0 commit comments

Comments
 (0)