defaultCamera method

Future<void> defaultCamera(
  1. BuildContext context,
  2. ShowCameraAction cameraAction,
  3. ScopeManager? scopeManager
)

Opens the platform default camera picker.

Implementation

Future<void> defaultCamera(BuildContext context,
    ShowCameraAction cameraAction, ScopeManager? scopeManager) async {
  final mode = Utils.getString(
      scopeManager?.dataContext.eval(cameraAction.options?['mode']),
      fallback: 'photo');

  final picker = ImagePicker();
  XFile? xFile;
  if (mode == 'video') {
    xFile = await picker.pickVideo(source: ImageSource.camera);
  } else if (mode == 'photo') {
    xFile = await picker.pickImage(source: ImageSource.camera);
  }

  if (xFile == null) return;
  final file = await convertXFile(xFile);
  if (file == null) return;

  if (cameraAction.id != null && scopeManager != null) {
    scopeManager.dataContext.addDataContext({
      cameraAction.id!: {
        'files': [file.toJson()]
      }
    });
  }
  if (cameraAction.id != null) {
    scopeManager?.dispatch(
      ModelChangeEvent(APIBindingSource(cameraAction.id!), {
        'files': [file.toJson()]
      }),
    );
  }
  if (cameraAction.onComplete != null) {
    try {
      // ignore: use_build_context_synchronously
      ScreenController().executeAction(context, cameraAction.onComplete!);
    } on Exception catch (_) {}
  }
}