Appearance
MutableRectangle<T extends num>
A class for representing two-dimensional axis-aligned rectangles with mutable properties.
Legacy: New usages of MutableRectangle are discouraged.
- If you are using the
MutableRectangleclass withdart:html, we recommend migrating topackage:web. To learn how and why to migrate, check out the migration guide. - If you want to store the boundaries of a rectangle in some coordinate system, consider using a record. Depending on how you will use it, this could look like
var boundaries = (mixX: x1, maxX: x2, minY: y1, maxY: y2). - If you need to perform intersection calculations or containment checks, consider using a dedicated library, such as
package:vector_math. - If you are developing a Flutter application or package, consider using the
Recttype fromdart:ui.
Implemented types
Constructors
MutableRectangle()
MutableRectangle(T left, T top, T width, T height)Create a mutable rectangle spanned by (left, top) and (left+width, top+height).
The rectangle contains the points with x-coordinate between left and left + width, and with y-coordinate between top and top + height, both inclusive.
The width and height should be non-negative. If width or height are negative, they are clamped to zero.
If width and height are zero, the "rectangle" comprises only the single point (left, top).
Example:
dart
var rectangle = MutableRectangle(20, 50, 300, 600);
print(rectangle); // Rectangle (20, 50) 300 x 600
print(rectangle.left); // 20
print(rectangle.top); // 50
print(rectangle.right); // 320
print(rectangle.bottom); // 650
// Change rectangle width and height.
rectangle.width = 200;
rectangle.height = 100;
print(rectangle); // Rectangle (20, 50) 200 x 100
print(rectangle.left); // 20
print(rectangle.top); // 50
print(rectangle.right); // 220
print(rectangle.bottom); // 150Legacy: New usages of MutableRectangle are discouraged. To learn more, check out the MutableRectangle class API docs.
Implementation
dart
MutableRectangle(this.left, this.top, T width, T height)
: this._width = (width < 0)
? _clampToZero<T>(width)
: (width + 0 as dynamic),
this._height = (height < 0)
? _clampToZero<T>(height)
: (height + 0 as dynamic);MutableRectangle.fromPoints() factory
Create a mutable rectangle spanned by the points a and b;
The rectangle contains the points with x-coordinate between a.x and b.x, and with y-coordinate between a.y and b.y, both inclusive.
If the distance between a.x and b.x is not representable (which can happen if one or both is a double), the actual right edge might be slightly off from max(a.x, b.x). Similar for the y-coordinates and the bottom edge.
Example:
dart
var leftTop = const Point(20, 50);
var rightBottom = const Point(300, 600);
var rectangle = MutableRectangle.fromPoints(leftTop, rightBottom);
print(rectangle); // Rectangle (20, 50) 280 x 550
print(rectangle.left); // 20
print(rectangle.top); // 50
print(rectangle.right); // 300
print(rectangle.bottom); // 600Implementation
dart
factory MutableRectangle.fromPoints(Point<T> a, Point<T> b) {
T left = min(a.x, b.x);
T width = (max(a.x, b.x) - left) as T;
T top = min(a.y, b.y);
T height = (max(a.y, b.y) - top) as T;
return MutableRectangle<T>(left, top, width, height);
}Properties
bottom no setter inherited
T get bottomInherited from _RectangleBase.
Implementation
dart
T get bottom => (top + height) as T;bottomLeft no setter inherited
Point<T> get bottomLeftInherited from _RectangleBase.
Implementation
dart
Point<T> get bottomLeft => Point<T>(this.left, (this.top + this.height) as T);bottomRight no setter inherited
Point<T> get bottomRightInherited from _RectangleBase.
Implementation
dart
Point<T> get bottomRight =>
Point<T>((this.left + this.width) as T, (this.top + this.height) as T);hashCode no setter inherited
int get hashCodeInherited from _RectangleBase.
Implementation
dart
int get hashCode => SystemHash.hash4(
left.hashCode,
top.hashCode,
right.hashCode,
bottom.hashCode,
0,
);height read / write
T get heightgetter:
The height of the rectangle.
setter:
Sets the height of the rectangle.
The height must be non-negative. If a negative height is supplied, it is clamped to zero.
Setting the value will change the bottom edge of the rectangle, but will not change top.
Implementation
dart
T get height => _height;
set height(T height) {
if (height < 0) height = _clampToZero<T>(height);
_height = height;
}left read / write
T leftgetter:
The x-coordinate of the left edge.
Setting the value will move the rectangle without changing its width.
setter:
The x-coordinate of the left edge.
Setting the value will move the rectangle without changing its width.
Implementation
dart
T left;right no setter inherited
T get rightInherited from _RectangleBase.
Implementation
dart
T get right => (left + width) as T;runtimeType no setter inherited
Type get runtimeTypeA representation of the runtime type of the object.
Inherited from Object.
Implementation
dart
external Type get runtimeType;top read / write
T topgetter:
The y-coordinate of the left edge.
Setting the value will move the rectangle without changing its height.
setter:
The y-coordinate of the left edge.
Setting the value will move the rectangle without changing its height.
Implementation
dart
T top;topLeft no setter inherited
Point<T> get topLeftInherited from _RectangleBase.
Implementation
dart
Point<T> get topLeft => Point<T>(this.left, this.top);topRight no setter inherited
Point<T> get topRightInherited from _RectangleBase.
Implementation
dart
Point<T> get topRight => Point<T>((this.left + this.width) as T, this.top);width read / write
T get widthgetter:
The width of the rectangle.
setter:
Sets the width of the rectangle.
The width must be non-negative. If a negative width is supplied, it is clamped to zero.
Setting the value will change the right edge of the rectangle, but will not change left.
Implementation
dart
T get width => _width;
set width(T width) {
if (width < 0) width = _clampToZero<T>(width);
_width = width;
}Methods
boundingBox() inherited
Returns a new rectangle which completely contains this and other.
Inherited from _RectangleBase.
Implementation
dart
Rectangle<T> boundingBox(Rectangle<T> other) {
var right = max(this.left + this.width, other.left + other.width);
var bottom = max(this.top + this.height, other.top + other.height);
var left = min(this.left, other.left);
var top = min(this.top, other.top);
return Rectangle<T>(left, top, (right - left) as T, (bottom - top) as T);
}containsPoint() inherited
Tests whether another is inside or along the edges of this.
Inherited from _RectangleBase.
Implementation
dart
bool containsPoint(Point<num> another) {
return another.x >= left &&
another.x <= left + width &&
another.y >= top &&
another.y <= top + height;
}containsRectangle() inherited
Tests whether this entirely contains another.
Inherited from _RectangleBase.
Implementation
dart
bool containsRectangle(Rectangle<num> another) {
return left <= another.left &&
left + width >= another.left + another.width &&
top <= another.top &&
top + height >= another.top + another.height;
}intersection() inherited
Computes the intersection of this and other.
The intersection of two axis-aligned rectangles, if any, is always another axis-aligned rectangle.
Returns the intersection of this and other, or null if they don't intersect.
Inherited from _RectangleBase.
Implementation
dart
Rectangle<T>? intersection(Rectangle<T> other) {
var x0 = max(left, other.left);
var x1 = min(left + width, other.left + other.width);
if (x0 <= x1) {
var y0 = max(top, other.top);
var y1 = min(top + height, other.top + other.height);
if (y0 <= y1) {
return Rectangle<T>(x0, y0, (x1 - x0) as T, (y1 - y0) as T);
}
}
return null;
}intersects() inherited
Returns true if this intersects other.
Inherited from _RectangleBase.
Implementation
dart
bool intersects(Rectangle<num> other) {
return (left <= other.left + other.width &&
other.left <= left + width &&
top <= other.top + other.height &&
other.top <= top + height);
}noSuchMethod() inherited
dynamic noSuchMethod(Invocation invocation)Invoked when a nonexistent method or property is accessed.
A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:
dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time errorThis invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).
Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.
A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:
dart
class MockList<T> implements List<T> {
noSuchMethod(Invocation invocation) {
log(invocation);
super.noSuchMethod(invocation); // Will throw.
}
}
void main() {
MockList().add(42);
}This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.
If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.
The default behavior is to throw a NoSuchMethodError.
Inherited from Object.
Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);toString() inherited
String toString()A string representation of this object.
Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.
Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.
Inherited from _RectangleBase.
Implementation
dart
String toString() {
return 'Rectangle ($left, $top) $width x $height';
}Operators
operator ==() inherited
The equality operator.
The default behavior for all Objects is to return true if and only if this object and other are the same object.
Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:
Total: It must return a boolean for all arguments. It should never throw.
Reflexive: For all objects
o,o == omust be true.Symmetric: For all objects
o1ando2,o1 == o2ando2 == o1must either both be true, or both be false.Transitive: For all objects
o1,o2, ando3, ifo1 == o2ando2 == o3are true, theno1 == o3must be true.
The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.
If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.
Inherited from _RectangleBase.
Implementation
dart
bool operator ==(Object other) =>
other is Rectangle &&
left == other.left &&
top == other.top &&
right == other.right &&
bottom == other.bottom;