Unverified Commit f777ead1 authored by timekone's avatar timekone Committed by GitHub

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

parent a1de2a22
...@@ -219,7 +219,10 @@ class BoxDecoration extends Decoration { ...@@ -219,7 +219,10 @@ class BoxDecoration extends Decoration {
Path? clipPath; Path? clipPath;
switch (shape) { switch (shape) {
case BoxShape.circle: 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; break;
case BoxShape.rectangle: case BoxShape.rectangle:
if (borderRadius != null) if (borderRadius != null)
...@@ -452,7 +455,11 @@ class _BoxDecorationPainter extends BoxPainter { ...@@ -452,7 +455,11 @@ class _BoxDecorationPainter extends BoxPainter {
Path? clipPath; Path? clipPath;
switch (_decoration.shape) { switch (_decoration.shape) {
case BoxShape.circle: 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; break;
case BoxShape.rectangle: case BoxShape.rectangle:
if (_decoration.borderRadius != null) if (_decoration.borderRadius != null)
......
...@@ -71,7 +71,7 @@ void main() { ...@@ -71,7 +71,7 @@ void main() {
); );
}); });
test('BoxDecoration.getClipPath', () { test('BoxDecoration.getClipPath with borderRadius', () {
const double radius = 10; const double radius = 10;
final BoxDecoration decoration = BoxDecoration( final BoxDecoration decoration = BoxDecoration(
borderRadius: BorderRadius.circular(radius), borderRadius: BorderRadius.circular(radius),
...@@ -84,4 +84,17 @@ void main() { ...@@ -84,4 +84,17 @@ void main() {
); );
expect(clipPath, isLookLikeExpectedPath); 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);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment