setFlashMode method

  1. @override
Future<void> setFlashMode(
  1. int cameraId,
  2. FlashMode mode
)

Sets the flash mode for the camera with ID cameraId.

When the FlashMode.torch is enabled, any previously set FlashMode with this method will be disabled, just as with any other FlashMode; while this is not default native Android behavior as defined by the CameraX API, this behavior is compliant with the plugin platform interface.

This method combines the notion of setting the flash mode of the imageCapture UseCase and enabling the camera torch, as described by https://developer.android.com/reference/androidx/camera/core/ImageCapture and https://developer.android.com/reference/androidx/camera/core/CameraControl#enableTorch(boolean), respectively.

Implementation

@override
Future<void> setFlashMode(int cameraId, FlashMode mode) async {
  // Turn off torch mode if it is enabled and not being redundantly set.
  if (mode != FlashMode.torch && torchEnabled) {
    await _enableTorchMode(false);
    torchEnabled = false;
  }

  switch (mode) {
    case FlashMode.off:
      _currentFlashMode = CameraXFlashMode.off;
    case FlashMode.auto:
      _currentFlashMode = CameraXFlashMode.auto;
    case FlashMode.always:
      _currentFlashMode = CameraXFlashMode.on;
    case FlashMode.torch:
      _currentFlashMode = null;
      if (torchEnabled) {
        // Torch mode enabled already.
        return;
      }

      await _enableTorchMode(true);
      torchEnabled = true;
  }
}