buildCameraPreview method

Widget buildCameraPreview({
  1. required BuildContext context,
  2. required DeviceOrientation orientation,
  3. required BoxConstraints constraints,
})

Implementation

Widget buildCameraPreview({
  required BuildContext context,
  required DeviceOrientation orientation,
  required BoxConstraints constraints,
}) {
  Widget preview = Listener(
    onPointerDown: (_) => pointers++,
    onPointerUp: (_) => pointers--,
    child: GestureDetector(
      onScaleStart: pickerConfig.enablePinchToZoom ? handleScaleStart : null,
      onScaleUpdate:
          pickerConfig.enablePinchToZoom ? handleScaleUpdate : null,
      // Enabled cameras switching by default if we have multiple cameras.
      onDoubleTap: cameras.length > 1 ? switchCameras : null,
      child: innerController != null
          ? CameraPreview(controller)
          : const SizedBox.shrink(),
    ),
  );

  // Make a transformed widget if it's defined.
  final Widget? transformedWidget =
      pickerConfig.previewTransformBuilder?.call(
    context,
    controller,
    preview,
  );
  preview = Center(child: transformedWidget ?? preview);
  // Scale the preview if the config is enabled.
  if (pickerConfig.enableScaledPreview) {
    preview = Transform.scale(
      scale: effectiveCameraScale(constraints, controller),
      child: preview,
    );
  }
  // Rotated the preview if the turns is valid.
  if (pickerConfig.cameraQuarterTurns % 4 != 0) {
    preview = RotatedBox(
      quarterTurns: -pickerConfig.cameraQuarterTurns,
      child: preview,
    );
  }
  return RepaintBoundary(child: preview);
}