Skip to content

Commit 34c6e54

Browse files
authored
🥅 Catch more errors with handler (#110)
1 parent 602ecb7 commit 34c6e54

File tree

3 files changed

+89
-76
lines changed

3 files changed

+89
-76
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ that can be found in the LICENSE file. -->
1212

1313
### Improvements
1414

15+
- Catch more errors with handler. (#110)
1516
- Improve tapping exposure updates. (#109)
1617
- Prevent unnecessary zoom updates. (#107)
1718

lib/src/widgets/camera_picker.dart

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,6 @@ class CameraPickerState extends State<CameraPicker>
134134
/// 在开始录像前,最后一次在拍照按钮按下的位置
135135
Offset? _lastShootingButtonPressedPosition;
136136

137-
/// Current exposure mode.
138-
/// 当前曝光模式
139-
final ValueNotifier<ExposureMode> _exposureMode =
140-
ValueNotifier<ExposureMode>(ExposureMode.auto);
141-
142137
final ValueNotifier<bool> _isExposureModeDisplays =
143138
ValueNotifier<bool>(false);
144139

@@ -288,7 +283,6 @@ class CameraPickerState extends State<CameraPicker>
288283
_controller?.dispose();
289284
_currentExposureOffset.dispose();
290285
_lastExposurePoint.dispose();
291-
_exposureMode.dispose();
292286
_isExposureModeDisplays.dispose();
293287
_exposurePointDisplayTimer?.cancel();
294288
_exposureModeDisplayTimer?.cancel();
@@ -477,18 +471,24 @@ class CameraPickerState extends State<CameraPicker>
477471
/// The method to switch between flash modes.
478472
/// 切换闪光灯模式的方法
479473
Future<void> switchFlashesMode() async {
474+
final FlashMode newFlashMode;
480475
switch (controller.value.flashMode) {
481476
case FlashMode.off:
482-
await controller.setFlashMode(FlashMode.auto);
477+
newFlashMode = FlashMode.auto;
483478
break;
484479
case FlashMode.auto:
485-
await controller.setFlashMode(FlashMode.always);
480+
newFlashMode = FlashMode.always;
486481
break;
487482
case FlashMode.always:
488483
case FlashMode.torch:
489-
await controller.setFlashMode(FlashMode.off);
484+
newFlashMode = FlashMode.off;
490485
break;
491486
}
487+
try {
488+
await controller.setFlashMode(newFlashMode);
489+
} catch (e, s) {
490+
handleErrorWithHandler(e, config.onError, s: s);
491+
}
492492
}
493493

494494
Future<void> zoom(double scale) async {
@@ -546,19 +546,25 @@ class CameraPickerState extends State<CameraPicker>
546546

547547
/// Use the specific [mode] to update the exposure mode.
548548
/// 设置曝光模式
549-
void switchExposureMode() {
550-
if (_exposureMode.value == ExposureMode.auto) {
551-
_exposureMode.value = ExposureMode.locked;
549+
Future<void> switchExposureMode() async {
550+
final ExposureMode mode = controller.value.exposureMode;
551+
final ExposureMode newMode;
552+
if (mode == ExposureMode.auto) {
553+
newMode = ExposureMode.locked;
552554
} else {
553-
_exposureMode.value = ExposureMode.auto;
555+
newMode = ExposureMode.auto;
554556
}
555557
_exposurePointDisplayTimer?.cancel();
556-
if (_exposureMode.value == ExposureMode.auto) {
558+
if (newMode == ExposureMode.auto) {
557559
_exposurePointDisplayTimer = Timer(const Duration(seconds: 5), () {
558560
_lastExposurePoint.value = null;
559561
});
560562
}
561-
controller.setExposureMode(_exposureMode.value);
563+
try {
564+
await controller.setExposureMode(newMode);
565+
} catch (e, s) {
566+
handleErrorWithHandler(e, config.onError, s: s);
567+
}
562568
_restartModeDisplayTimer();
563569
}
564570

@@ -581,23 +587,22 @@ class CameraPickerState extends State<CameraPicker>
581587
_lastExposurePoint.value = position;
582588
_restartPointDisplayTimer();
583589
_currentExposureOffset.value = 0;
584-
if (_exposureMode.value == ExposureMode.locked) {
585-
await controller.setExposureMode(ExposureMode.auto);
586-
_exposureMode.value = ExposureMode.auto;
587-
}
588-
controller.setExposurePoint(
589-
_lastExposurePoint.value!.scale(
590+
try {
591+
if (controller.value.exposureMode == ExposureMode.locked) {
592+
await controller.setExposureMode(ExposureMode.auto);
593+
}
594+
final Offset newPoint = _lastExposurePoint.value!.scale(
590595
1 / constraints.maxWidth,
591596
1 / constraints.maxHeight,
592-
),
593-
);
594-
if (controller.value.focusPointSupported) {
595-
controller.setFocusPoint(
596-
_lastExposurePoint.value!.scale(
597-
1 / constraints.maxWidth,
598-
1 / constraints.maxHeight,
599-
),
600597
);
598+
if (controller.value.exposurePointSupported) {
599+
controller.setExposurePoint(newPoint);
600+
}
601+
if (controller.value.focusPointSupported) {
602+
controller.setFocusPoint(newPoint);
603+
}
604+
} catch (e, s) {
605+
handleErrorWithHandler(e, config.onError, s: s);
601606
}
602607
}
603608

@@ -1185,43 +1190,42 @@ class CameraPickerState extends State<CameraPicker>
11851190

11861191
/// The area widget for the last exposure point that user manually set.
11871192
/// 用户手动设置的曝光点的区域显示
1188-
Widget _focusingAreaWidget(BoxConstraints constraints) {
1193+
Widget _focusingAreaWidget(
1194+
CameraValue cameraValue,
1195+
BoxConstraints constraints,
1196+
) {
11891197
Widget _buildControl(double size, double height) {
11901198
const double verticalGap = 3;
1191-
return ValueListenableBuilder<ExposureMode>(
1192-
valueListenable: _exposureMode,
1193-
builder: (_, ExposureMode mode, __) {
1194-
final bool isLocked = mode == ExposureMode.locked;
1195-
return Column(
1196-
children: <Widget>[
1197-
ValueListenableBuilder<bool>(
1198-
valueListenable: _isExposureModeDisplays,
1199-
builder: (_, bool value, Widget? child) => AnimatedOpacity(
1200-
duration: _kDuration,
1201-
opacity: value ? 1 : 0,
1202-
child: child,
1203-
),
1204-
child: GestureDetector(
1205-
onTap: switchExposureMode,
1206-
child: SizedBox.fromSize(
1207-
size: Size.square(size),
1208-
child: Icon(
1209-
isLocked ? Icons.lock_rounded : Icons.lock_open_rounded,
1210-
size: size,
1211-
color: isLocked ? _lockedColor : null,
1212-
),
1213-
),
1199+
final ExposureMode exposureMode = cameraValue.exposureMode;
1200+
final bool isLocked = exposureMode == ExposureMode.locked;
1201+
return Column(
1202+
children: <Widget>[
1203+
ValueListenableBuilder<bool>(
1204+
valueListenable: _isExposureModeDisplays,
1205+
builder: (_, bool value, Widget? child) => AnimatedOpacity(
1206+
duration: _kDuration,
1207+
opacity: value ? 1 : 0,
1208+
child: child,
1209+
),
1210+
child: GestureDetector(
1211+
onTap: switchExposureMode,
1212+
child: SizedBox.fromSize(
1213+
size: Size.square(size),
1214+
child: Icon(
1215+
isLocked ? Icons.lock_rounded : Icons.lock_open_rounded,
1216+
size: size,
1217+
color: isLocked ? _lockedColor : null,
12141218
),
12151219
),
1216-
const SizedBox(height: verticalGap),
1217-
Expanded(
1218-
child: _exposureSlider(mode, size, height, verticalGap),
1219-
),
1220-
const SizedBox(height: verticalGap),
1221-
SizedBox.fromSize(size: Size.square(size)),
1222-
],
1223-
);
1224-
},
1220+
),
1221+
),
1222+
const SizedBox(height: verticalGap),
1223+
Expanded(
1224+
child: _exposureSlider(exposureMode, size, height, verticalGap),
1225+
),
1226+
const SizedBox(height: verticalGap),
1227+
SizedBox.fromSize(size: Size.square(size)),
1228+
],
12251229
);
12261230
}
12271231

@@ -1450,7 +1454,9 @@ class CameraPickerState extends State<CameraPicker>
14501454
if (enableSetExposure)
14511455
_exposureDetectorWidget(c, constraints),
14521456
_initializeWrapper(
1453-
builder: (_, __) => _focusingAreaWidget(constraints),
1457+
builder: (CameraValue cameraValue, Widget? w) {
1458+
return _focusingAreaWidget(cameraValue, constraints);
1459+
},
14541460
),
14551461
_contentBuilder(constraints),
14561462
if (config.foregroundBuilder != null)

lib/src/widgets/camera_picker_viewer.dart

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ class _CameraPickerViewerState extends State<CameraPickerViewer> {
167167
if (shouldAutoPreviewVideo) {
168168
videoController.play();
169169
}
170-
} catch (e) {
170+
} catch (e, s) {
171171
hasErrorWhenInitializing = true;
172172
realDebugPrint('Error when initializing video controller: $e');
173-
handleErrorWithHandler(e, widget.onError);
173+
handleErrorWithHandler(e, widget.onError, s: s);
174174
} finally {
175175
if (mounted) {
176176
setState(() {});
@@ -193,16 +193,20 @@ class _CameraPickerViewerState extends State<CameraPickerViewer> {
193193
/// the end, then click the button will make the video replay.
194194
/// 一般来说按钮只切换播放暂停。当视频播放结束时,点击按钮将从头开始播放。
195195
Future<void> playButtonCallback() async {
196-
if (isPlaying.value) {
197-
videoController.pause();
198-
} else {
199-
if (videoController.value.duration == videoController.value.position) {
200-
videoController
201-
..seekTo(Duration.zero)
202-
..play();
196+
try {
197+
if (isPlaying.value) {
198+
videoController.pause();
203199
} else {
204-
videoController.play();
200+
if (videoController.value.duration == videoController.value.position) {
201+
videoController
202+
..seekTo(Duration.zero)
203+
..play();
204+
} else {
205+
videoController.play();
206+
}
205207
}
208+
} catch (e, s) {
209+
handleErrorWithHandler(e, widget.onError, s: s);
206210
}
207211
}
208212

@@ -244,11 +248,13 @@ class _CameraPickerViewerState extends State<CameraPickerViewer> {
244248
),
245249
widget.onError,
246250
);
247-
} catch (e) {
251+
} catch (e, s) {
248252
realDebugPrint('Saving entity failed: $e');
249-
handleErrorWithHandler(e, widget.onError);
253+
handleErrorWithHandler(e, widget.onError, s: s);
250254
} finally {
251-
Navigator.of(context).pop(entity);
255+
if (mounted) {
256+
Navigator.of(context).pop(entity);
257+
}
252258
}
253259
}
254260

0 commit comments

Comments
 (0)