Add Rect.fromCenter() constructor (#8716)

This commit is contained in:
Todd Volkert
2019-04-25 14:40:29 -07:00
committed by GitHub
parent 74abe2965b
commit 0c9c293b56
3 changed files with 50 additions and 10 deletions
+14 -5
View File
@@ -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
+14 -5
View File
@@ -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
+22
View File
@@ -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);
});
}