toPicture method

Picture? toPicture({
  1. int width = 512,
  2. int height = 256,
  3. Color? color,
  4. Color? background,
  5. double? strokeWidth,
  6. double? maxStrokeWidth,
  7. double border = 0.0,
  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? strokeWidth,
  double? maxStrokeWidth,
  double border = 0.0,
  bool fit = true,
}) {
  if (!isFilled) {
    return null;
  }

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

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

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

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

  final recorder = PictureRecorder();
  final painter = PathSignaturePainter(
    paths: [path],
    color: color,
    width: strokeWidth,
    maxWidth: maxStrokeWidth,
    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();
}