setGrid method
Set different sizes for each of the fixed size rows and columns
A nine-box is a grid with 3 rows and 3 columns. The outer-most columns,
leftWidth
and rightWidth
, are a fixed-width. As the nine-box is
resized, those columns remain fixed-width and the center column stretches
to take up the remaining space. In the same way, the outer-most rows,
topHeight
and bottomHeight
, are a fixed-height. As the nine-box is
resized, those rows remain fixed-height and the center row stretches
to take up the remaining space.
Any widths or heights that are not specified remain unchanged.
Implementation
void setGrid({
double? leftWidth,
double? rightWidth,
double? topHeight,
double? bottomHeight,
}) {
if (leftWidth != null && rightWidth != null) {
assert(
leftWidth + rightWidth <= sprite.src.width,
'The left and right columns ($leftWidth + $rightWidth) do '
'not fit in the width of the sprite (${sprite.src.width})',
);
} else if (leftWidth != null) {
assert(
leftWidth <= center.right,
'The left column ($leftWidth) is too large '
'(max ${center.right})',
);
} else if (rightWidth != null) {
assert(
rightWidth + center.left <= sprite.src.width,
'The right column ($rightWidth) is too large '
'(max ${sprite.src.width - center.left})',
);
}
if (topHeight != null && bottomHeight != null) {
assert(
topHeight + bottomHeight <= sprite.src.height,
'The top and bottom rows ($topHeight + $bottomHeight) do not fit '
'in the height of the sprite (${sprite.src.height})',
);
} else if (topHeight != null) {
assert(
topHeight <= center.bottom,
'The top row ($topHeight) is too large '
'(max ${center.bottom})',
);
} else if (bottomHeight != null) {
assert(
bottomHeight + center.top <= sprite.src.height,
'The bottom row ($bottomHeight) is too large '
'(max ${sprite.src.height - center.top})',
);
}
final left = leftWidth ?? center.left;
final top = topHeight ?? center.top;
late final double right;
if (rightWidth == null) {
right = center.right;
} else {
right = sprite.src.width - rightWidth;
}
late final double bottom;
if (bottomHeight == null) {
bottom = center.bottom;
} else {
bottom = sprite.src.height - bottomHeight;
}
center = Rect.fromLTRB(
left,
top,
right,
bottom,
);
}