paint method

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

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 (_firstRow == null) {
    assert(_lastRow == null);
    return;
  }
  assert(_firstRow != null && _lastRow != null);

  if (_animationLeadingIndices.isEmpty) {
    // There are no animations running. Clip only if there is visual overflow.
    if (_hasVisualOverflow && clipBehavior != Clip.none) {
      _clipHandles[_viewportClipKey] ??= LayerHandle<ClipRectLayer>();
      _clipHandles[_viewportClipKey]!.layer = context.pushClipRect(
        needsCompositing,
        offset,
        Offset.zero & size,
        (PaintingContext context, Offset offset) {
          _paintRows(
            context,
            offset,
            leadingRow: _firstRow!,
            trailingRow: _lastRow!,
          );
        },
        clipBehavior: clipBehavior,
        oldLayer: _clipHandles[_viewportClipKey]!.layer,
      );
    } else {
      _clipHandles[_viewportClipKey]?.layer = null;
      _paintRows(
        context,
        offset,
        leadingRow: _firstRow!,
        trailingRow: _lastRow!,
      );
    }
    return;
  }

  // We are animating.
  // Separate animating segments to clip for any overlap.
  int leadingIndex = _firstRow!;
  final List<int> animationIndices = _animationLeadingIndices.keys.toList()
    ..sort();
  final List<_PaintSegment> paintSegments = <_PaintSegment>[];
  while (animationIndices.isNotEmpty) {
    final int trailingIndex = animationIndices.removeAt(0);
    paintSegments.add((
      leadingIndex: leadingIndex,
      trailingIndex: trailingIndex - 1,
    ));
    leadingIndex = trailingIndex;
  }
  paintSegments.add((leadingIndex: leadingIndex, trailingIndex: _lastRow!));

  // Paint, clipping for all but the first segment, unless there is visual
  // overflow.
  final _PaintSegment firstSegment = paintSegments.removeAt(0);
  if (_hasVisualOverflow && clipBehavior != Clip.none) {
    _clipHandles[_viewportClipKey] ??= LayerHandle<ClipRectLayer>();
    _clipHandles[_viewportClipKey]!.layer = context.pushClipRect(
      needsCompositing,
      offset,
      Offset.zero & size,
      (PaintingContext context, Offset offset) {
        _paintRows(
          context,
          offset,
          leadingRow: firstSegment.leadingIndex,
          trailingRow: firstSegment.trailingIndex,
        );
      },
      clipBehavior: clipBehavior,
      oldLayer: _clipHandles[_viewportClipKey]!.layer,
    );
  } else {
    _clipHandles[_viewportClipKey]?.layer = null;
    _paintRows(
      context,
      offset,
      leadingRow: firstSegment.leadingIndex,
      trailingRow: firstSegment.trailingIndex,
    );
  }
  // Paint the rest with clip layers.
  while (paintSegments.isNotEmpty) {
    final _PaintSegment segment = paintSegments.removeAt(0);
    final int parentIndex = segment.leadingIndex - 1;
    final double leadingOffset = _rowMetrics[parentIndex]!.trailingOffset;
    final double trailingOffset =
        _rowMetrics[segment.trailingIndex]!.trailingOffset;
    final Rect rect = Rect.fromPoints(
      Offset(0.0, leadingOffset - verticalOffset.pixels),
      Offset(
        viewportDimension.width,
        math.min(
          trailingOffset - verticalOffset.pixels,
          viewportDimension.height,
        ),
      ),
    );
    // We use the same animation key to keep track of the clip layer, unless
    // this is the odd man out segment.
    final UniqueKey key = _animationLeadingIndices[leadingIndex]!;
    _clipHandles[key] ??= LayerHandle<ClipRectLayer>();
    _clipHandles[key]!.layer = context.pushClipRect(
      needsCompositing,
      offset,
      rect,
      (PaintingContext context, Offset offset) {
        _paintRows(
          context,
          offset,
          leadingRow: segment.leadingIndex,
          trailingRow: segment.trailingIndex,
        );
      },
      oldLayer: _clipHandles[key]!.layer,
    );
  }
}