setRegion method

Future<void> setRegion({
  1. required Image image,
  2. int x = 0,
  3. int y = 0,
})

Implementation

Future<void> setRegion({
  required Image image,
  int x = 0,
  int y = 0,
}) async {
  if (pixels == null) {
    throw Exception('Must call loadPixels() before calling updatePixels()');
  }

  // In theory, this method should copy each pixels in the given image
  // into the pixels buffer. But, it's easier for us to utilize the Canvas
  // to accomplish the same thing. To use the Canvas, we must first ensure
  // that any intermediate values in the pixels buffer are applied back to
  // the intermediate image. For example, if the user called set() on any
  // pixels but has not yet called updatePixels(), those pixels would be
  // lost during an intermediate rasterization. Therefore, the first thing
  // we do is updatePixels().
  await updatePixels();

  // Use the Canvas to draw the given image at the desired offset.
  _paintingContext.canvas.drawImage(image, Offset.zero, Paint());
  _paintingContext.markHasUnappliedCanvasCommands();

  // Rasterize the Canvas image command and load the latest image data
  // into the pixels buffer.
  await _paintingContext.loadPixels();
}