createImageSync method

Image createImageSync([
  1. bool adjustOffset = true,
  2. double resolution = 1,
  3. GRect? rect
])

Creates a synchronous image from this display object.

adjustOffset determines whether to adjust the offset of the generated image to the top-left corner of the display object's bounds. If true, the generated image will have its origin at (0, 0) and its size will be equal to the bounds of the display object. Defaults to true.

resolution determines the resolution of the generated image. Defaults to 1.

rect determines the bounds of the image to generate. Defaults to null, which means that the method will use the filter bounds of the display object.

Returns a ui.Image instance.

Implementation

ui.Image createImageSync([
  bool adjustOffset = true,
  double resolution = 1,
  GRect? rect,
]) {
  rect ??= getFilterBounds();
  rect = rect!.clone();
  if (resolution != 1) {
    rect *= resolution;
  }
  final needsAdjust =
      (rect.left != 0 || rect.top != 0) && adjustOffset || resolution != 1;
  late ui.Picture picture;
  if (needsAdjust) {
    picture = createPicture((canvas) {
      if (adjustOffset) canvas.translate(-rect!.left, -rect.top);
      if (resolution != 1) canvas.scale(resolution);
    }, (canvas) {
      if (adjustOffset) canvas.restore();
      if (resolution != 1) canvas.restore();
    });
  } else {
    picture = createPicture();
  }
  final width = adjustOffset ? rect.width.toInt() : rect.right.toInt();
  final height = adjustOffset ? rect.height.toInt() : rect.bottom.toInt();
  final output = picture.toImageSync(width, height);
  picture.dispose();
  return output;
}