offStage static method

Future offStage(
  1. Widget widget, {
  2. Duration? wait,
  3. bool openFilePreview = true,
  4. bool saveToDevice = false,
  5. String fileName = 'davinci',
  6. String? albumName,
  7. double? pixelRatio,
  8. required BuildContext context,
  9. BrandTagConfiguration? brandTag,
  10. bool returnImageUint8List = false,
})
  • If the widget is not in the widget tree, use this method. if the fileName is not set, it sets the file name as "davinci". you can define whether to openFilePreview or returnImageUint8List If the image is blurry, calculate the pixelratio dynamically. See the readme for more info on how to do it. Context is required.

Implementation

static Future offStage(Widget widget,
    {Duration? wait,
    bool openFilePreview = true,
    bool saveToDevice = false,
    String fileName = 'davinci',
    String? albumName,
    double? pixelRatio,
    required BuildContext context,
    BrandTagConfiguration? brandTag,
    bool returnImageUint8List = false}) async {
  /// finding the widget in the current context by the key.
  final RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary();

  /// create a new pipeline owner
  final PipelineOwner pipelineOwner = PipelineOwner();

  /// create a new build owner
  final BuildOwner buildOwner = BuildOwner(focusManager: FocusManager());

  Size logicalSize =
      View.of(context).physicalSize / View.of(context).devicePixelRatio;
  pixelRatio ??= View.of(context).devicePixelRatio;
  try {
    final RenderView renderView = RenderView(
      view: View.of(context),
      child: RenderPositionedBox(
          alignment: Alignment.center, child: repaintBoundary),
      configuration: ViewConfiguration(
        size: logicalSize,
        devicePixelRatio: 1.0,
      ),
    );

    /// setting the rootNode to the renderview of the widget
    pipelineOwner.rootNode = renderView;

    /// setting the renderView to prepareInitialFrame
    renderView.prepareInitialFrame();

    /// setting the rootElement with the widget that has to be captured
    final RenderObjectToWidgetElement<RenderBox> rootElement =
        RenderObjectToWidgetAdapter<RenderBox>(
      container: repaintBoundary,
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: Column(
          // image is center aligned
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            widget,
            // if the brandTag is available, it's gets added as footer
            if (brandTag != null) BrandTagBuilder(tagConfiguration: brandTag),
          ],
        ),
      ),
    ).attachToRenderTree(buildOwner);

    ///adding the rootElement to the buildScope
    buildOwner.buildScope(rootElement);

    /// if the wait is null, sometimes
    /// the then waiting for the given [wait] amount of time and
    /// then creating an image via a [RepaintBoundary].

    if (wait != null) {
      await Future.delayed(wait);
    }

    ///adding the rootElement to the buildScope
    buildOwner.buildScope(rootElement);

    /// finialize the buildOwner
    buildOwner.finalizeTree();

    ///Flush Layout
    pipelineOwner.flushLayout();

    /// Flush Compositing Bits
    pipelineOwner.flushCompositingBits();

    /// Flush paint
    pipelineOwner.flushPaint();

    /// we start the createImageProcess once we have the repaintBoundry of
    /// the widget we attached to the widget tree.
    return await _createImageProcess(
        saveToDevice: saveToDevice,
        albumName: albumName,
        fileName: fileName,
        returnImageUint8List: returnImageUint8List,
        source: _Source.offStage,
        openFilePreview: openFilePreview,
        repaintBoundary: repaintBoundary,
        pixelRatio: pixelRatio);
  } catch (e) {
    print(e);
  }
}