create static method

CircleImage create({
  1. required double diameter,
  2. required Color color,
  3. Path? shapePath,
})

Creates a circular ui.Image of the given diameter and color.

The image is drawn using a ui.PictureRecorder and ui.Canvas. The resulting ui.Image has a width and height equal to diameter.

Parameters:

  • diameter: The diameter of the circle in pixels.
  • color: The fill color of the circle.

Returns: A ui.Image containing a circle of the specified size and color.

Example Usage:

final image = CircleImageFactory.create(
  diameter: 100.0,
  color: ui.Color(0xFFFF0000), // Red color
);

Implementation

static CircleImage create({
  required double diameter,
  required ui.Color color,
  ui.Path? shapePath,
}) {
  // Create a PictureRecorder to record drawing commands
  final recorder = ui.PictureRecorder();
  final canvas = ui.Canvas(recorder);
  canvas.drawColor(const ui.Color(0x00000000), ui.BlendMode.clear);

  // Prepare the paint with the given color
  final paint = ui.Paint()..color = color;

  // Calculate the radius
  final radius = diameter / 2;
  final center = ui.Offset(radius, radius);

  if (shapePath != null) {
    _drawCustomPath(canvas, shapePath, center, diameter, paint);
  } else {
    canvas.drawCircle(center, radius, paint);
  }

  // End recording and convert it to an image
  final picture = recorder.endRecording();
  return CircleImage(
    image: picture.toImageSync(diameter.toInt(), diameter.toInt()),
    color: color,
    dimension: diameter,
  );
}