diff --git a/packages/flutter/lib/src/painting/box_decoration.dart b/packages/flutter/lib/src/painting/box_decoration.dart index c766c230f7..4c41e961bc 100644 --- a/packages/flutter/lib/src/painting/box_decoration.dart +++ b/packages/flutter/lib/src/painting/box_decoration.dart @@ -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) diff --git a/packages/flutter/test/painting/box_decoration_test.dart b/packages/flutter/test/painting/box_decoration_test.dart index 6f74976684..44a8eeb432 100644 --- a/packages/flutter/test/painting/box_decoration_test.dart +++ b/packages/flutter/test/painting/box_decoration_test.dart @@ -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(50.0, 0.0), Offset(40.0, 10.0), ], + excludes: const [ Offset(40.0, 0.0), Offset(10.0, 10.0), ], + ); + expect(clipPath, isLookLikeExpectedPath); + }); }