switchFlashesMode method

Future<void> switchFlashesMode(
  1. CameraValue value
)

The method to switch between flash modes. 切换闪光灯模式的方法

Implementation

Future<void> switchFlashesMode(CameraValue value) async {
  final List<FlashMode> flashModes = validFlashModes[currentCamera]!;
  if (flashModes.isEmpty) {
    // Unlikely event that no flash mode is valid for current camera.
    handleErrorWithHandler(
      CameraException(
        'No FlashMode found.',
        'No flash modes are available with the camera.',
      ),
      StackTrace.current,
      pickerConfig.onError,
    );
    return;
  }
  final int currentFlashModeIndex = flashModes.indexOf(value.flashMode);
  final int nextFlashModeIndex;
  if (currentFlashModeIndex + 1 >= flashModes.length) {
    nextFlashModeIndex = 0;
  } else {
    nextFlashModeIndex = currentFlashModeIndex + 1;
  }
  final FlashMode nextFlashMode = flashModes[nextFlashModeIndex];
  try {
    await controller.setFlashMode(nextFlashMode);
  } catch (e, s) {
    // Remove the flash mode that throws an exception.
    validFlashModes[currentCamera]!.remove(nextFlashMode);
    switchFlashesMode(value);
    handleErrorWithHandler(e, s, pickerConfig.onError);
  }
}