paintInterior method

  1. @override
void paintInterior(
  1. Canvas canvas,
  2. Rect rect,
  3. Paint paint, {
  4. TextDirection? textDirection,
})
override

Paint a canvas with the appropriate shape.

On ShapeBorder subclasses whose preferPaintInterior method returns true, this should be faster than using Canvas.drawPath with the path provided by getOuterPath. (If preferPaintInterior returns false, then this method asserts in debug mode and does nothing in release mode.)

Subclasses are expected to implement this method when the Canvas API has a dedicated method to draw the relevant shape. For example, CircleBorder uses this to call Canvas.drawCircle, and RoundedRectangleBorder uses this to call Canvas.drawRRect.

Subclasses that implement this must ensure that calling paintInterior is semantically equivalent to (i.e. renders the same pixels as) calling Canvas.drawPath with the same Paint and the Path returned from getOuterPath, and must also override preferPaintInterior to return true.

For example, a shape that draws a rectangle might implement getOuterPath, paintInterior, and preferPaintInterior as follows:

class RectangleBorder extends OutlinedBorder {
  // ...

  @override
  Path getOuterPath(Rect rect, { TextDirection? textDirection }) {
   return Path()
     ..addRect(rect);
  }

  @override
  void paintInterior(Canvas canvas, Rect rect, Paint paint, {TextDirection? textDirection}) {
   canvas.drawRect(rect, paint);
  }

  @override
  bool get preferPaintInterior => true;

  // ...
}

When a shape can only be drawn using path, preferPaintInterior must return false. In that case, classes such as ShapeDecoration will cache the path from getOuterPath and call Canvas.drawPath directly.

Implementation

@override
void paintInterior(Canvas canvas, Rect rect, Paint paint, { TextDirection? textDirection }) {
  if (eccentricity == 0.0) {
    canvas.drawCircle(rect.center, rect.shortestSide / 2.0, paint);
  } else {
    canvas.drawOval(_adjustRect(rect), paint);
  }
}