Skip to content

Commit

Permalink
Merge pull request #7 from letsar/fix-borders
Browse files Browse the repository at this point in the history
Fixes an issue with BorderAlign
  • Loading branch information
aloisdeniel committed Sep 27, 2022
2 parents 8ea82c4 + 640131a commit db49913
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 42 deletions.
4 changes: 2 additions & 2 deletions lib/src/smooth_border_radius.dart
Expand Up @@ -177,7 +177,7 @@ class SmoothBorderRadius extends BorderRadius {

/// Returns the difference between two [BorderRadius] objects.
SmoothBorderRadius operator -(BorderRadius other) {
if (other is SmoothRadius)
if (other is SmoothBorderRadius)
return SmoothBorderRadius.only(
topLeft: (topLeft - other.topLeft) as SmoothRadius,
topRight: (topRight - other.topRight) as SmoothRadius,
Expand All @@ -190,7 +190,7 @@ class SmoothBorderRadius extends BorderRadius {

/// Returns the sum of two [BorderRadius] objects.
SmoothBorderRadius operator +(BorderRadius other) {
if (other is SmoothRadius) {
if (other is SmoothBorderRadius) {
return SmoothBorderRadius.only(
topLeft: (topLeft + other.topLeft) as SmoothRadius,
topRight: (topRight + other.topRight) as SmoothRadius,
Expand Down
101 changes: 61 additions & 40 deletions lib/src/smooth_rectangle_border.dart
Expand Up @@ -25,7 +25,16 @@ class SmoothRectangleBorder extends OutlinedBorder {
final BorderAlign borderAlign;

@override
EdgeInsetsGeometry get dimensions => EdgeInsets.all(side.width);
EdgeInsetsGeometry get dimensions {
switch (borderAlign) {
case BorderAlign.inside:
return EdgeInsets.all(side.width);
case BorderAlign.center:
return EdgeInsets.all(side.width / 2);
case BorderAlign.outside:
return EdgeInsets.zero;
}
}

@override
ShapeBorder scale(double t) {
Expand Down Expand Up @@ -102,45 +111,20 @@ class SmoothRectangleBorder extends OutlinedBorder {

@override
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
final outerRect = () {
switch (borderAlign) {
case BorderAlign.inside:
return rect;
case BorderAlign.center:
return rect.inflate(side.width / 2);
case BorderAlign.outside:
return rect.inflate(side.width);
}
}();
final radius = () {
switch (borderAlign) {
case BorderAlign.inside:
return borderRadius;
case BorderAlign.center:
return borderRadius +
SmoothBorderRadius.all(
SmoothRadius(
cornerRadius: side.width / 2,
cornerSmoothing: 1.0,
),
);
case BorderAlign.outside:
return borderRadius +
SmoothBorderRadius.all(
SmoothRadius(
cornerRadius: side.width,
cornerSmoothing: 1.0,
),
);
}
}();
return _getPath(rect, borderRadius, textDirection: textDirection);
}

Path _getPath(
Rect rect,
SmoothBorderRadius radius, {
TextDirection? textDirection,
}) {
if ([radius.bottomLeft, radius.bottomRight, radius.topLeft, radius.topRight]
.every((x) => x.cornerSmoothing == 0.0)) {
return Path()..addRRect(radius.resolve(textDirection).toRRect(outerRect));
return Path()..addRRect(radius.resolve(textDirection).toRRect(rect));
}

return radius.toPath(outerRect);
return radius.toPath(rect);
}

@override
Expand All @@ -163,14 +147,51 @@ class SmoothRectangleBorder extends OutlinedBorder {
case BorderStyle.none:
break;
case BorderStyle.solid:
final outerPath = getOuterPath(
rect,
// Since the stroke is painted at the center, we need to adjust the rect
// according to the [borderAlign].
final adjustedRect = () {
switch (borderAlign) {
case BorderAlign.inside:
return rect.deflate(side.width / 2);
case BorderAlign.center:
return rect;
case BorderAlign.outside:
return rect.inflate(side.width / 2);
}
}();
final adjustedBorderRadius = () {
switch (borderAlign) {
case BorderAlign.inside:
return borderRadius -
SmoothBorderRadius.all(
SmoothRadius(
cornerRadius: side.width / 2,
cornerSmoothing: 1.0,
),
);
case BorderAlign.center:
return borderRadius;
case BorderAlign.outside:
return borderRadius +
SmoothBorderRadius.all(
SmoothRadius(
cornerRadius: side.width / 2,
cornerSmoothing: 1.0,
),
);
}
}();

final outerPath = _getPath(
adjustedRect,
adjustedBorderRadius,
textDirection: textDirection,
);

final paint = side.toPaint();

canvas.drawPath(outerPath, paint);
canvas.drawPath(
outerPath,
side.toPaint(),
);

break;
}
Expand Down

0 comments on commit db49913

Please sign in to comment.