paint method

  1. @override
void paint(
  1. Canvas canvas,
  2. Rect rect,
  3. {TextDirection? textDirection,
  4. BoxShape shape = BoxShape.rectangle,
  5. BorderRadius? borderRadius}
)
override

Paints the border within the given Rect on the given Canvas.

Uniform borders and non-uniform borders with similar colors and styles are more efficient to paint than more complex borders.

You can provide a BoxShape to draw the border on. If the shape in BoxShape.circle, there is the requirement that the border has uniform color and style.

If you specify a rectangular box shape (BoxShape.rectangle), then you may specify a BorderRadius. If a borderRadius is specified, there is the requirement that the border has uniform color and style.

The getInnerPath and getOuterPath methods do not know about the shape and borderRadius arguments.

The textDirection argument is not used by this paint method.

See also:

  • paintBorder, which is used if the border has non-uniform colors or styles and no borderRadius.
  • pub.dev/packages/non_uniform_border, a package that implements a Non-Uniform Border on ShapeBorder, which is used by Material Design buttons and other widgets, under the "shape" field.

Implementation

@override
void paint(
  Canvas canvas,
  Rect rect, {
  TextDirection? textDirection,
  BoxShape shape = BoxShape.rectangle,
  BorderRadius? borderRadius,
}) {
  if (isUniform) {
    switch (top.style) {
      case BorderStyle.none:
        return;
      case BorderStyle.solid:
        switch (shape) {
          case BoxShape.circle:
            assert(borderRadius == null, 'A borderRadius cannot be given when shape is a BoxShape.circle.');
            BoxBorder._paintUniformBorderWithCircle(canvas, rect, top);
          case BoxShape.rectangle:
            if (borderRadius != null && borderRadius != BorderRadius.zero) {
              BoxBorder._paintUniformBorderWithRadius(canvas, rect, top, borderRadius);
              return;
            }
            BoxBorder._paintUniformBorderWithRectangle(canvas, rect, top);
        }
        return;
    }
  }

  if (_styleIsUniform && top.style == BorderStyle.none) {
    return;
  }

  // Allow painting non-uniform borders if the visible colors are uniform.
  final Set<Color> visibleColors = _distinctVisibleColors();
  final bool hasHairlineBorder = _hasHairlineBorder;
  // Paint a non uniform border if a single color is visible
  // and (borderRadius is present) or (border is visible and width != 0.0).
  if (visibleColors.length == 1 &&
      !hasHairlineBorder &&
      (shape == BoxShape.circle ||
          (borderRadius != null && borderRadius != BorderRadius.zero))) {
    BoxBorder.paintNonUniformBorder(canvas, rect,
        shape: shape,
        borderRadius: borderRadius,
        textDirection: textDirection,
        top: top.style == BorderStyle.none ? BorderSide.none : top,
        right: right.style == BorderStyle.none ? BorderSide.none : right,
        bottom: bottom.style == BorderStyle.none ? BorderSide.none : bottom,
        left: left.style == BorderStyle.none ? BorderSide.none : left,
        color: visibleColors.first);
    return;
  }

   assert(() {
    if (hasHairlineBorder) {
      assert(borderRadius == null || borderRadius == BorderRadius.zero,
          'A hairline border like `BorderSide(width: 0.0, style: BorderStyle.solid)` can only be drawn when BorderRadius is zero or null.');
    }
    if (borderRadius != null && borderRadius != BorderRadius.zero) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('A borderRadius can only be given on borders with uniform colors.'),
        ErrorDescription('The following is not uniform:'),
        if (!_colorIsUniform) ErrorDescription('BorderSide.color'),
      ]);
    }
    return true;
  }());
  assert(() {
    if (shape != BoxShape.rectangle) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('A Border can only be drawn as a circle on borders with uniform colors.'),
        ErrorDescription('The following is not uniform:'),
        if (!_colorIsUniform) ErrorDescription('BorderSide.color'),
      ]);
    }
    return true;
  }());
  assert(() {
    if (!_strokeAlignIsUniform || top.strokeAlign != BorderSide.strokeAlignInside) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('A Border can only draw strokeAlign different than BorderSide.strokeAlignInside on borders with uniform colors.'),
      ]);
    }
    return true;
  }());

  paintBorder(canvas, rect, top: top, right: right, bottom: bottom, left: left);
}