resizeImage static method

Image resizeImage(
  1. Image image,
  2. int targetWidth,
  3. int targetHeight, {
  4. int x = 0,
  5. int y = 0,
})

Resizes an ui.Image to a given targetWidth and targetHeight

Implementation

static ui.Image resizeImage(
  ui.Image image,
  int targetWidth,
  int targetHeight, {
  int x = 0,
  int y = 0,
}) {
  if (image.width == targetWidth && image.height == targetHeight) {
    return image;
  }
  final recorder = ui.PictureRecorder();
  final canvas = ui.Canvas(recorder);

  canvas.drawImageRect(
    image,
    ui.Rect.fromLTRB(x.toDouble(), y.toDouble(), image.width.toDouble(),
        image.height.toDouble()),
    ui.Rect.fromLTRB(0, 0, targetWidth.toDouble(), targetHeight.toDouble()),
    ui.Paint(),
  );

  return recorder.endRecording().toImageSync(targetWidth, targetHeight);
}