doneEditing method

void doneEditing()

Complete the editing process and return the edited image.

This function is called when the user is done editing the image. If no changes have been made or if the image has no additional layers, it cancels the editing process and closes the editor. Otherwise, it captures the current state of the image, including any applied changes or layers, and returns it as a byte array.

Before returning the edited image, a loading dialog is displayed to indicate that the operation is in progress.

Implementation

void doneEditing() async {
  if (_processFinalImage) return;
  if (stateManager.position <= 0 && activeLayers.isEmpty) {
    if (!imageGenerationConfigs.allowEmptyEditCompletion) {
      return closeEditor();
    }
  }
  callbacks.onImageEditingStarted?.call();

  /// Hide every unnessacary element that Screenshot Controller will capture a correct image.
  setState(() {
    _processFinalImage = true;
    layerInteractionManager.selectedLayerId = '';
    _checkInteractiveViewer();
  });

  /// Ensure hero animations finished
  if (isSubEditorOpen) await _pageOpenCompleter.future;

  WidgetsBinding.instance.addPostFrameCallback((_) async {
    LoadingDialog loading = LoadingDialog();
    await loading.show(
      context,
      theme: _theme,
      configs: configs,
      message: i18n.doneLoadingMsg,
    );

    if (callbacks.onThumbnailGenerated != null) {
      if (_imageInfos == null) await decodeImage();

      final List<dynamic> results = await Future.wait([
        captureEditorImage(),
        _controllers.screenshot.getOriginalImage(imageInfos: _imageInfos!),
      ]);

      await callbacks.onThumbnailGenerated!(results[0], results[1]);
    } else {
      Uint8List? bytes = await captureEditorImage();
      await onImageEditingComplete?.call(bytes);
    }

    if (mounted) loading.hide(context);

    onCloseEditor?.call();

    /// Allow users to continue editing if they didn't close the editor.
    setState(() => _processFinalImage = false);
  });
}