takePicture method

Future<void> takePicture()

The method to take a picture. 拍照方法

The picture will only taken when isInitialized, and the camera is not taking pictures. 仅当初始化成功且相机未在拍照时拍照。

Implementation

Future<void> takePicture() async {
  if (!controller.value.isInitialized) {
    handleErrorWithHandler(
      StateError('Camera has not initialized.'),
      widget.onError,
    );
  }
  if (controller.value.isTakingPicture) {
    return;
  }
  try {
    final XFile _file = await controller.takePicture();
    // Delay disposing the controller to hold the preview.
    Future<void>.delayed(const Duration(milliseconds: 500), () {
      controller.dispose();
    });
    final AssetEntity? entity = await CameraPickerViewer.pushToViewer(
      context,
      pickerState: this,
      pickerType: CameraPickerViewType.image,
      previewXFile: _file,
      theme: theme,
      shouldDeletePreviewFile: shouldDeletePreviewFile,
      shouldAutoPreviewVideo: shouldAutoPreviewVideo,
      onEntitySaving: widget.onEntitySaving,
    );
    if (entity != null) {
      Navigator.of(context).pop(entity);
      return;
    }
    initCameras(currentCamera);
    safeSetState(() {});
  } catch (e) {
    realDebugPrint('Error when preview the captured file: $e');
    handleErrorWithHandler(e, widget.onError);
  }
}