paint method

  1. @override
void paint(
  1. PaintingContext context,
  2. Offset offset
)
inherited

Paint this render object into the given context at the given offset.

Subclasses should override this method to provide a visual appearance for themselves. The render object's local coordinate system is axis-aligned with the coordinate system of the context's canvas and the render object's local origin (i.e, x=0 and y=0) is placed at the given offset in the context's canvas.

Do not call this function directly. If you wish to paint yourself, call markNeedsPaint instead to schedule a call to this function. If you wish to paint one of your children, call PaintingContext.paintChild on the given context.

When painting one of your children (via a paint child function on the given context), the current canvas held by the context might change because draw operations before and after painting children might need to be recorded on separate compositing layers.

Implementation

@override
void paint(PaintingContext context, Offset offset) {
  if (isPlaying) {
    // Paint again
    if (_frameCallbackID != -1) {
      SchedulerBinding.instance?.cancelFrameCallbackWithId(_frameCallbackID);
    }
    _frameCallbackID =
        SchedulerBinding.instance?.scheduleFrameCallback(_beginFrame) ?? -1;
  }

  final Canvas canvas = context.canvas;

  AABB bounds = aabb;
  double contentWidth = bounds[2] - bounds[0];
  double contentHeight = bounds[3] - bounds[1];
  double x = -1 * bounds[0] -
      contentWidth / 2.0 -
      (_alignment.x * contentWidth / 2.0);
  double y = -1 * bounds[1] -
      contentHeight / 2.0 -
      (_alignment.y * contentHeight / 2.0);

  double scaleX = 1.0, scaleY = 1.0;

  canvas.save();
  prePaint(canvas, offset);

  switch (_fit) {
    case BoxFit.fill:
      scaleX = size.width / contentWidth;
      scaleY = size.height / contentHeight;
      break;
    case BoxFit.contain:
      double minScale =
          min(size.width / contentWidth, size.height / contentHeight);
      scaleX = scaleY = minScale;
      break;
    case BoxFit.cover:
      double maxScale =
          max(size.width / contentWidth, size.height / contentHeight);
      scaleX = scaleY = maxScale;
      break;
    case BoxFit.fitHeight:
      double minScale = size.height / contentHeight;
      scaleX = scaleY = minScale;
      break;
    case BoxFit.fitWidth:
      double minScale = size.width / contentWidth;
      scaleX = scaleY = minScale;
      break;
    case BoxFit.none:
      scaleX = scaleY = 1.0;
      break;
    case BoxFit.scaleDown:
      double minScale =
          min(size.width / contentWidth, size.height / contentHeight);
      scaleX = scaleY = minScale < 1.0 ? minScale : 1.0;
      break;
  }

  Mat2D transform = Mat2D();
  transform[4] =
      offset.dx + size.width / 2.0 + (_alignment.x * size.width / 2.0);
  transform[5] =
      offset.dy + size.height / 2.0 + (_alignment.y * size.height / 2.0);
  Mat2D.scale(transform, transform, Vec2D.fromValues(scaleX, scaleY));
  Mat2D center = Mat2D();
  center[4] = x;
  center[5] = y;
  Mat2D.multiply(transform, transform, center);

  canvas.translate(
    offset.dx + size.width / 2.0 + (_alignment.x * size.width / 2.0),
    offset.dy + size.height / 2.0 + (_alignment.y * size.height / 2.0),
  );

  canvas.scale(scaleX, scaleY);
  canvas.translate(x, y);

  paintFlare(canvas, transform);

  canvas.restore();
  postPaint(canvas, offset);
}