toImage method

Future<Image?> toImage()

convert to

Implementation

Future<ui.Image?> toImage() async {
  if (isEmpty) {
    return null;
  }

  double minX = double.infinity, minY = double.infinity;
  double maxX = 0, maxY = 0;
  for (Point point in points) {
    if (point.offset.dx < minX) {
      minX = point.offset.dx;
    }
    if (point.offset.dy < minY) {
      minY = point.offset.dy;
    }
    if (point.offset.dx > maxX) {
      maxX = point.offset.dx;
    }
    if (point.offset.dy > maxY) {
      maxY = point.offset.dy;
    }
  }

  final ui.PictureRecorder recorder = ui.PictureRecorder();
  final ui.Canvas canvas = Canvas(recorder)
    ..translate(-(minX - penStrokeWidth), -(minY - penStrokeWidth));
  if (exportBackgroundColor != null) {
    final ui.Paint paint = Paint()..color = exportBackgroundColor!;
    canvas.drawPaint(paint);
  }
  SignaturePainter(this).paint(canvas, Size.infinite);
  final ui.Picture picture = recorder.endRecording();
  return picture.toImage(
    (maxX - minX + penStrokeWidth * 2).toInt(),
    (maxY - minY + penStrokeWidth * 2).toInt(),
  );
}