Skip to content

Commit edaeac8

Browse files
lcuisAlexV525
andauthored
Proposed solution to #154 [BUG] iOS front camera flash can be set ... (#156)
This is a proposed solution to #154 [BUG] iOS front camera flash can be set from auto to on but not further . This solves the unsupported flash modes exception by falling back on the next mode with an infinite loop prevention control (just in case no flash mode is supported). Co-authored-by: Alex Li <[email protected]>
1 parent 3933b99 commit edaeac8

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ that can be found in the LICENSE file. -->
44

55
# Changelog
66

7+
## 3.7.0
8+
9+
### Improvements
10+
11+
- Allow flash modes failed to switch and can move on to next when switching. (#156)
12+
713
## 3.6.5
814

915
### Fixes

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: wechat_camera_picker_demo
22
description: A new Flutter project.
3-
version: 3.6.5+21
3+
version: 3.7.0+22
44
publish_to: none
55

66
environment:

lib/src/states/camera_picker_state.dart

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ class CameraPickerState extends State<CameraPicker>
114114
/// 但如果录像时间没有限制,定时器将不会起作用。
115115
Timer? recordCountdownTimer;
116116

117+
/// Initialized with all the flash modes for each camera. If a flash mode is
118+
/// not valid, it is removed from the list.
119+
/// 使用每个相机的所有闪光灯模式进行初始化。
120+
/// 如果闪光灯模式无效,则将其从列表中删除。
121+
final Map<CameraDescription, List<FlashMode>> validFlashModes =
122+
<CameraDescription, List<FlashMode>>{};
123+
117124
////////////////////////////////////////////////////////////////////////////
118125
////////////////////////////// Global Getters //////////////////////////////
119126
////////////////////////////////////////////////////////////////////////////
@@ -272,6 +279,7 @@ class CameraPickerState extends State<CameraPicker>
272279
);
273280
}
274281

282+
initFlashModesForCameras();
275283
final int preferredIndex = cameras.indexWhere(
276284
(CameraDescription e) =>
277285
e.lensDirection == pickerConfig.preferredLensDirection,
@@ -343,6 +351,24 @@ class CameraPickerState extends State<CameraPicker>
343351
});
344352
}
345353

354+
/// Initializes the flash modes in [validFlashModes] for each
355+
/// [CameraDescription].
356+
/// 为每个 [CameraDescription][validFlashModes] 中初始化闪光灯模式。
357+
void initFlashModesForCameras() {
358+
for (final CameraDescription camera in cameras) {
359+
if (!validFlashModes.containsKey(camera)) {
360+
// Mind the order of this list as it has an impact on the switch cycle.
361+
// Do not use FlashMode.values.
362+
validFlashModes[camera] = <FlashMode>[
363+
FlashMode.auto,
364+
FlashMode.always,
365+
FlashMode.torch,
366+
FlashMode.off,
367+
];
368+
}
369+
}
370+
}
371+
346372
/// Switch cameras in order. When the [currentCameraIndex] reached the length
347373
/// of cameras, start from the beginning.
348374
/// 按顺序切换相机。当达到相机数量时从头开始。
@@ -369,25 +395,33 @@ class CameraPickerState extends State<CameraPicker>
369395

370396
/// The method to switch between flash modes.
371397
/// 切换闪光灯模式的方法
372-
Future<void> switchFlashesMode() async {
373-
final FlashMode newFlashMode;
374-
switch (controller.value.flashMode) {
375-
case FlashMode.off:
376-
newFlashMode = FlashMode.auto;
377-
break;
378-
case FlashMode.auto:
379-
newFlashMode = FlashMode.always;
380-
break;
381-
case FlashMode.always:
382-
newFlashMode = FlashMode.torch;
383-
break;
384-
case FlashMode.torch:
385-
newFlashMode = FlashMode.off;
386-
break;
398+
Future<void> switchFlashesMode(CameraValue value) async {
399+
final List<FlashMode> flashModes = validFlashModes[currentCamera]!;
400+
if (flashModes.isEmpty) {
401+
// Unlikely event that no flash mode is valid for current camera.
402+
handleErrorWithHandler(
403+
CameraException(
404+
'No FlashMode found.',
405+
'No flash modes are available with the camera.',
406+
),
407+
pickerConfig.onError,
408+
);
409+
return;
410+
}
411+
final int currentFlashModeIndex = flashModes.indexOf(value.flashMode);
412+
final int nextFlashModeIndex;
413+
if (currentFlashModeIndex + 1 >= flashModes.length) {
414+
nextFlashModeIndex = 0;
415+
} else {
416+
nextFlashModeIndex = currentFlashModeIndex + 1;
387417
}
418+
final FlashMode nextFlashMode = flashModes[nextFlashModeIndex];
388419
try {
389-
await controller.setFlashMode(newFlashMode);
420+
await controller.setFlashMode(nextFlashMode);
390421
} catch (e, s) {
422+
// Remove the flash mode that throws an exception.
423+
validFlashModes[currentCamera]!.remove(nextFlashMode);
424+
switchFlashesMode(value);
391425
handleErrorWithHandler(e, pickerConfig.onError, s: s);
392426
}
393427
}
@@ -863,7 +897,7 @@ class CameraPickerState extends State<CameraPicker>
863897
break;
864898
}
865899
return IconButton(
866-
onPressed: switchFlashesMode,
900+
onPressed: () => switchFlashesMode(value),
867901
tooltip: textDelegate.sFlashModeLabel(value.flashMode),
868902
icon: Icon(icon, size: 24),
869903
);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: wechat_camera_picker
22
description: A camera picker based on WeChat's UI which is a separate runnable extension to wechat_assets_picker.
33
repository: https://github.com/fluttercandies/flutter_wechat_camera_picker
4-
version: 3.6.5
4+
version: 3.7.0
55

66
environment:
77
sdk: ">=2.14.0 <3.0.0"

0 commit comments

Comments
 (0)