setFlatChildren method
Replaces the children of this table with the given cells.
The cells are divided into the specified number of columns before replacing the existing children.
If the new cells contain any existing children of the table, those children are moved to their new location in the table rather than removed from the table and re-added.
Implementation
void setFlatChildren(int columns, List<RenderBox?> cells) {
if (cells == _children && columns == _columns) {
return;
}
assert(columns >= 0);
// consider the case of a newly empty table
if (columns == 0 || cells.isEmpty) {
assert(cells.isEmpty);
_columns = columns;
if (_children.isEmpty) {
assert(_rows == 0);
return;
}
for (final RenderBox? oldChild in _children) {
if (oldChild != null) {
dropChild(oldChild);
}
}
_rows = 0;
_children.clear();
markNeedsLayout();
return;
}
assert(cells.length % columns == 0);
// fill a set with the cells that are moving (it's important not
// to dropChild a child that's remaining with us, because that
// would clear their parentData field)
final Set<RenderBox> lostChildren = HashSet<RenderBox>();
for (int y = 0; y < _rows; y += 1) {
for (int x = 0; x < _columns; x += 1) {
final int xyOld = x + y * _columns;
final int xyNew = x + y * columns;
if (_children[xyOld] != null && (x >= columns || xyNew >= cells.length || _children[xyOld] != cells[xyNew])) {
lostChildren.add(_children[xyOld]!);
}
}
}
// adopt cells that are arriving, and cross cells that are just moving off our list of lostChildren
int y = 0;
while (y * columns < cells.length) {
for (int x = 0; x < columns; x += 1) {
final int xyNew = x + y * columns;
final int xyOld = x + y * _columns;
if (cells[xyNew] != null && (x >= _columns || y >= _rows || _children[xyOld] != cells[xyNew])) {
if (!lostChildren.remove(cells[xyNew])) {
adoptChild(cells[xyNew]!);
}
}
}
y += 1;
}
// drop all the lost children
lostChildren.forEach(dropChild);
// update our internal values
_columns = columns;
_rows = cells.length ~/ columns;
_children = List<RenderBox?>.of(cells);
assert(_children.length == rows * columns);
markNeedsLayout();
}