paint method Null safety

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

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.


void paint(PaintingContext context, Offset offset) {
  if (firstChild == null)
  // offset is to the top-left corner, regardless of our axis direction.
  // originOffset gives us the delta from the real origin to the origin in the axis direction.
  final Offset mainAxisUnit, crossAxisUnit, originOffset;
  final bool addExtent;
  switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {
    case AxisDirection.up:
      mainAxisUnit = const Offset(0.0, -1.0);
      crossAxisUnit = const Offset(1.0, 0.0);
      originOffset = offset + Offset(0.0, geometry!.paintExtent);
      addExtent = true;
    case AxisDirection.right:
      mainAxisUnit = const Offset(1.0, 0.0);
      crossAxisUnit = const Offset(0.0, 1.0);
      originOffset = offset;
      addExtent = false;
    case AxisDirection.down:
      mainAxisUnit = const Offset(0.0, 1.0);
      crossAxisUnit = const Offset(1.0, 0.0);
      originOffset = offset;
      addExtent = false;
    case AxisDirection.left:
      mainAxisUnit = const Offset(-1.0, 0.0);
      crossAxisUnit = const Offset(0.0, 1.0);
      originOffset = offset + Offset(geometry!.paintExtent, 0.0);
      addExtent = true;
  assert(mainAxisUnit != null);
  assert(addExtent != null);
  RenderBox? child = firstChild;
  while (child != null) {
    final double mainAxisDelta = childMainAxisPosition(child);
    final double crossAxisDelta = childCrossAxisPosition(child);
    Offset childOffset = Offset(
      originOffset.dx + mainAxisUnit.dx * mainAxisDelta + crossAxisUnit.dx * crossAxisDelta,
      originOffset.dy + mainAxisUnit.dy * mainAxisDelta + crossAxisUnit.dy * crossAxisDelta,
    if (addExtent)
      childOffset += mainAxisUnit * paintExtentOf(child);

    // If the child's visible interval (mainAxisDelta, mainAxisDelta + paintExtentOf(child))
    // does not intersect the paint extent interval (0, constraints.remainingPaintExtent), it's hidden.
    if (mainAxisDelta < constraints.remainingPaintExtent && mainAxisDelta + paintExtentOf(child) > 0)
      context.paintChild(child, childOffset);

    child = childAfter(child);