buildCameraPreview method

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

Implementation

Widget buildCameraPreview({
  required BuildContext context,
  required CameraValue cameraValue,
  required BoxConstraints constraints,
}) {
  Widget preview = const SizedBox.shrink();
  if (innerController != null) {
    preview = CameraPreview(controller);
    preview = ValueListenableBuilder<CameraValue>(
      valueListenable: controller,
      builder: (_, CameraValue value, Widget? child) {
        final lockedOrientation = value.lockedCaptureOrientation;
        int? quarterTurns = lockedOrientation?.index;
        if (quarterTurns == null) {
          return child!;
        }
        if (value.deviceOrientation == DeviceOrientation.landscapeLeft) {
          quarterTurns--;
        } else if (value.deviceOrientation ==
            DeviceOrientation.landscapeRight) {
          quarterTurns++;
        }
        return RotatedBox(quarterTurns: quarterTurns, child: child);
      },
      child: preview,
    );
  }
  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: preview,
    ),
  );

  // Make a transformed widget if it's defined.
  final Widget? transformedWidget =
      pickerConfig.previewTransformBuilder?.call(
    context,
    controller,
    preview,
  );
  if (!enableScaledPreview) {
    preview = Stack(
      children: <Widget>[
        preview,
        Positioned.fill(
          child: ExcludeSemantics(
            child: RotatedBox(
              quarterTurns: cameraQuarterTurns,
              child: Align(
                alignment: {
                  DeviceOrientation.portraitUp: Alignment.bottomCenter,
                  DeviceOrientation.portraitDown: Alignment.topCenter,
                  DeviceOrientation.landscapeLeft: Alignment.centerRight,
                  DeviceOrientation.landscapeRight: Alignment.centerLeft,
                }[cameraValue.deviceOrientation]!,
                child: buildCaptureTips(innerController),
              ),
            ),
          ),
        ),
        if (pickerConfig.enableSetExposure)
          buildExposureDetector(context, constraints),
        buildFocusingPoint(
          cameraValue: cameraValue,
          constraints: constraints,
          quarterTurns: cameraQuarterTurns,
        ),
        if (pickerConfig.foregroundBuilder != null)
          Positioned.fill(
            child: pickerConfig.foregroundBuilder!(
              context,
              innerController,
            ),
          ),
      ],
    );
  }
  // Scale the preview if the config is enabled.
  if (enableScaledPreview) {
    preview = Transform.scale(
      scale: effectiveCameraScale(constraints, innerController),
      child: Center(child: transformedWidget ?? preview),
    );
    // Rotated the preview if the turns is valid.
    if (isCameraRotated) {
      preview = RotatedBox(
        quarterTurns: -cameraQuarterTurns,
        child: preview,
      );
    }
  }
  return RepaintBoundary(child: preview);
}