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) {
  final child = this.child;
  if (child == null) return;
  final p = _paragraph;
  if (p != null && p.hasSize) {
    final (fragments, lines) = _cut(p);
    final shift = MatrixUtils.getAsTranslation(p.getTransformTo(this));
    if (fragments.isNotEmpty && shift != null) {
      final canvas = context.canvas;
      final at = offset + shift;
      // Where the paragraph is on the page, which the edges are snapped on.
      final page = localToGlobal(shift);
      double snap(double v) => (v + 0.5).floorToDouble();
      for (final f in fragments) {
        final g = f.box.shift(page);
        f.box = Rect.fromLTRB(snap(g.left), snap(g.top), snap(g.right), snap(g.bottom)).shift(-page);
      }
      // The strip each fragment is cut from: the box's fragments set end
      // to end, from where this one's predecessors would start.
      final spans = Map<InlineFrameSpan, List<_Fragment>>.identity();
      for (final f in fragments) {
        (spans[f.span] ??= []).add(f);
      }
      for (final group in spans.values) {
        final length = group.fold(0.0, (t, f) => t + f.box.width);
        var before = 0.0;
        for (final f in group) {
          f.strip = Rect.fromLTWH(f.box.left - before, f.box.top, length, f.box.height);
          before += f.box.width;
        }
      }
      void frame(_Fragment f) {
        if (f.box.isEmpty) return;
        final painter = f.span.decoration.createBoxPainter();
        final strip = f.strip.shift(at);
        canvas
          ..save()
          ..clipRect(f.box.shift(at));
        painter.paint(canvas, strip.topLeft, ImageConfiguration(size: strip.size, textDirection: p.textDirection));
        canvas.restore();
        painter.dispose();
      }

      final tops = lines.keys.toList()..sort();
      final copies = p.firstChild == null && _reachesUp(fragments, lines) ? _copies(p, tops.length) : null;
      if (copies != null) {
        // Line by line: its frames, then its text. The paragraph's own
        // text is all in the copies.
        for (final (i, top) in tops.indexed) {
          for (final f in fragments) {
            if (f.line == top) frame(f);
          }
          copies[i].paint(canvas, at);
        }
        return;
      }
      fragments.forEach(frame);
    }
  }
  context.paintChild(child, offset);
}