switchCameraSensor method

Future<void> switchCameraSensor({
  1. CameraAspectRatios? aspectRatio,
  2. double? zoom,
  3. FlashMode? flash,
  4. SensorType? type,
})

Switch camera from Sensors.BACK Sensors.front All states can switch this

Implementation

Future<void> switchCameraSensor({
  CameraAspectRatios? aspectRatio,
  double? zoom,
  FlashMode? flash,
  SensorType? type,
}) async {
  final previous = cameraContext.sensorConfig;

  SensorConfig next;
  if (previous.sensors.length <= 1) {
    next = SensorConfig.single(
      sensor: previous.sensors.first.position == SensorPosition.back
          ? Sensor.position(SensorPosition.front)
          : Sensor.position(SensorPosition.back),
      // TODO Initial values are not set in native when set like this
      aspectRatio: aspectRatio ?? CameraAspectRatios.ratio_4_3,
      zoom: zoom ?? 0.0,
      flashMode: flash ?? FlashMode.none,
    );
  } else {
    // switch all camera position in array by one like this:
    // old: [front, telephoto, wide]
    // new : [wide, front, telephoto]
    final newSensorsCopy = [...previous.sensors.whereNotNull()];
    next = SensorConfig.multiple(
      sensors: newSensorsCopy..insert(0, newSensorsCopy.removeAt(newSensorsCopy.length - 1)),
      // TODO Initial values are not set in native when set like this
      aspectRatio: aspectRatio ?? CameraAspectRatios.ratio_4_3,
      zoom: zoom ?? 0.0,
      flashMode: flash ?? FlashMode.none,
    );
  }
  await cameraContext.setSensorConfig(next);

  // TODO Once initial sensorConfig is correctly handled, we can remove below lines
  if (aspectRatio != null) {
    await next.setAspectRatio(aspectRatio);
  }
  if (zoom != null) {
    await next.setZoom(zoom);
  }
  if (flash != null) {
    await next.setFlashMode(flash);
  }
}