diff --git a/lib/stub_ui/lib/geometry.dart b/lib/stub_ui/lib/geometry.dart index 35f04ee07..17f18bbab 100644 --- a/lib/stub_ui/lib/geometry.dart +++ b/lib/stub_ui/lib/geometry.dart @@ -636,11 +636,20 @@ class Rect { /// Construct a rectangle that bounds the given circle. /// /// The `center` argument is assumed to be an offset from the origin. - Rect.fromCircle({ Offset center, double radius }) : this.fromLTRB( - center.dx - radius, - center.dy - radius, - center.dx + radius, - center.dy + radius, + Rect.fromCircle({ Offset center, double radius }) : this.fromCenter( + center: center, + width: radius * 2, + height: radius * 2, + ); + + /// Constructs a rectangle from its center point, width, and height. + /// + /// The `center` argument is assumed to be an offset from the origin. + Rect.fromCenter({ Offset center, double width, double height }) : this.fromLTRB( + center.dx - width / 2, + center.dy - height / 2, + center.dx + width / 2, + center.dy + height / 2, ); /// Construct the smallest rectangle that encloses the given offsets, treating diff --git a/lib/ui/geometry.dart b/lib/ui/geometry.dart index 22298e723..0369f30c7 100644 --- a/lib/ui/geometry.dart +++ b/lib/ui/geometry.dart @@ -637,11 +637,20 @@ class Rect { /// Construct a rectangle that bounds the given circle. /// /// The `center` argument is assumed to be an offset from the origin. - Rect.fromCircle({ Offset center, double radius }) : this.fromLTRB( - center.dx - radius, - center.dy - radius, - center.dx + radius, - center.dy + radius, + Rect.fromCircle({ Offset center, double radius }) : this.fromCenter( + center: center, + width: radius * 2, + height: radius * 2, + ); + + /// Constructs a rectangle from its center point, width, and height. + /// + /// The `center` argument is assumed to be an offset from the origin. + Rect.fromCenter({ Offset center, double width, double height }) : this.fromLTRB( + center.dx - width / 2, + center.dy - height / 2, + center.dx + width / 2, + center.dy + height / 2, ); /// Construct the smallest rectangle that encloses the given offsets, treating diff --git a/testing/dart/geometry_test.dart b/testing/dart/geometry_test.dart index 20445b862..3b87568d1 100644 --- a/testing/dart/geometry_test.dart +++ b/testing/dart/geometry_test.dart @@ -90,4 +90,26 @@ void main() { expect(const Size(-1.0, -1.0).aspectRatio, 1.0); expect(const Size(3.0, 4.0).aspectRatio, 3.0 / 4.0); }); + test('Rect.fromCenter', () { + Rect rect = Rect.fromCenter(center: const Offset(1.0, 3.0), width: 5.0, height: 7.0); + expect(rect.left, -1.5); + expect(rect.top, -0.5); + expect(rect.right, 3.5); + expect(rect.bottom, 6.5); + rect = Rect.fromCenter(center: const Offset(0.0, 0.0), width: 0.0, height: 0.0); + expect(rect.left, 0.0); + expect(rect.top, 0.0); + expect(rect.right, 0.0); + expect(rect.bottom, 0.0); + rect = Rect.fromCenter(center: const Offset(double.nan, 0.0), width: 0.0, height: 0.0); + expect(rect.left, isNaN); + expect(rect.top, 0.0); + expect(rect.right, isNaN); + expect(rect.bottom, 0.0); + rect = Rect.fromCenter(center: const Offset(0.0, double.nan), width: 0.0, height: 0.0); + expect(rect.left, 0.0); + expect(rect.top, isNaN); + expect(rect.right, 0.0); + expect(rect.bottom, isNaN); + }); }