Clip image of BoxDecoration to circle when shape is BoxShape.circle (#63352)

This commit is contained in:
timekone
2020-08-21 03:21:05 +03:00
committed by GitHub
parent a1de2a22fb
commit f777ead154
2 changed files with 23 additions and 3 deletions

View File

@@ -219,7 +219,10 @@ class BoxDecoration extends Decoration {
Path? clipPath;
switch (shape) {
case BoxShape.circle:
clipPath = Path()..addOval(rect);
final Offset center = rect.center;
final double radius = rect.shortestSide / 2.0;
final Rect square = Rect.fromCircle(center: center, radius: radius);
clipPath = Path()..addOval(square);
break;
case BoxShape.rectangle:
if (borderRadius != null)
@@ -452,7 +455,11 @@ class _BoxDecorationPainter extends BoxPainter {
Path? clipPath;
switch (_decoration.shape) {
case BoxShape.circle:
clipPath = Path()..addOval(rect);
assert(_decoration.borderRadius == null);
final Offset center = rect.center;
final double radius = rect.shortestSide / 2.0;
final Rect square = Rect.fromCircle(center: center, radius: radius);
clipPath = Path()..addOval(square);
break;
case BoxShape.rectangle:
if (_decoration.borderRadius != null)

View File

@@ -71,7 +71,7 @@ void main() {
);
});
test('BoxDecoration.getClipPath', () {
test('BoxDecoration.getClipPath with borderRadius', () {
const double radius = 10;
final BoxDecoration decoration = BoxDecoration(
borderRadius: BorderRadius.circular(radius),
@@ -84,4 +84,17 @@ void main() {
);
expect(clipPath, isLookLikeExpectedPath);
});
test('BoxDecoration.getClipPath with shape BoxShape.circle', () {
const BoxDecoration decoration = BoxDecoration(
shape: BoxShape.circle,
);
const Rect rect = Rect.fromLTWH(0.0, 0.0, 100.0, 20.0);
final Path clipPath = decoration.getClipPath(rect, TextDirection.ltr);
final Matcher isLookLikeExpectedPath = isPathThat(
includes: const <Offset>[ Offset(50.0, 0.0), Offset(40.0, 10.0), ],
excludes: const <Offset>[ Offset(40.0, 0.0), Offset(10.0, 10.0), ],
);
expect(clipPath, isLookLikeExpectedPath);
});
}