toPicture method

Picture? toPicture({
  1. int width = 512,
  2. int height = 256,
  3. Color? color,
  4. Color? background,
  5. double? size,
  6. double? maxSize,
  7. double? border,
  8. bool fit = true,
})

Exports data to Picture.

If fit is enabled, the path will be normalized and scaled to fit given width and height.

Implementation

Picture? toPicture({
  int width: 512,
  int height: 256,
  Color? color,
  Color? background,
  double? size,
  double? maxSize,
  double? border,
  bool fit: true,
}) {
  if (!isFilled) {
    return null;
  }

  final pictureRect = Rect.fromLTRB(
    0.0,
    0.0,
    width.toDouble(),
    height.toDouble(),
  );

  final canvasRect = Rect.fromLTRB(0, 0, _areaSize.width, _areaSize.height);
  final data = fit
      ? PathUtil.fill(_arcs, pictureRect, border: border)
      : PathUtil.fill(_arcs, pictureRect, bound: canvasRect, border: border);
  final path = CubicPath().._arcs.addAll(data);

  params ??= SignaturePaintParams(
    color: Colors.black,
    strokeWidth: 1.0,
    maxStrokeWidth: 10.0,
  );

  color ??= params!.color;
  size ??= params!.strokeWidth;
  maxSize ??= params!.maxStrokeWidth;

  final recorder = PictureRecorder();
  final painter = PathSignaturePainter(
    paths: [path],
    color: color,
    width: size,
    maxWidth: maxSize,
    type: SignatureDrawType.arc,
  );

  final canvas = Canvas(
    recorder,
    Rect.fromPoints(
      Offset(0.0, 0.0),
      Offset(width.toDouble(), height.toDouble()),
    ),
  );

  if (background != null) {
    canvas.drawColor(background, BlendMode.src);
  }

  painter.paint(canvas, Size(width.toDouble(), height.toDouble()));

  return recorder.endRecording();
}