updateChildren method
Makes oldChildren show newWidgets and returns the elements that do,
in order; each child's slot is an IndexedSlot. As in Flutter, the
matching heads of both lists are updated in place, then the middles,
where an old child with a key goes on to the new widget with that key
and the other old children are removed, then the matching tails. So a
child inserted or removed leaves its siblings their elements, and
children with keys keep theirs, render objects moved, when reordered.
The forgottenChildren among oldChildren, which forgetChild let
go, are no longer there: they are neither updated nor removed.
Implementation
@protected
List<Element> updateChildren(
List<Element> oldChildren,
List<Widget> newWidgets, {
Set<Element>? forgottenChildren,
}) {
var oldTop = 0;
var newTop = 0;
var oldBottom = oldChildren.length - 1;
var newBottom = newWidgets.length - 1;
// Flutter always makes a new list. Here, with as many new widgets as
// old children, the old list is written over: every index is read
// (the head and tail as they are reached, the middle before any is
// written) before it is written.
final inPlace = oldChildren.length == newWidgets.length;
final newChildren = inPlace ? oldChildren : <Element>[];
Element? previous;
while (oldTop <= oldBottom &&
newTop <= newBottom &&
!_forgotten(forgottenChildren, oldChildren[oldTop]) &&
Widget.canUpdate(oldChildren[oldTop].widget, newWidgets[newTop])) {
final child = oldChildren[oldTop];
previous = updateChild(
child,
newWidgets[newTop],
_slotAt(child, newTop, previous),
)!;
if (inPlace) {
newChildren[newTop] = previous;
} else {
newChildren.add(previous);
}
oldTop++;
newTop++;
}
// the tails are updated last, when the siblings before them are known
while (oldTop <= oldBottom &&
newTop <= newBottom &&
Widget.canUpdate(
oldChildren[oldBottom].widget,
newWidgets[newBottom],
)) {
oldBottom--;
newBottom--;
}
// made only for a middle of old children left
Map<Key, Element>? keyed;
for (var i = oldTop; i <= oldBottom; i++) {
final child = oldChildren[i];
final key = child.widget.key;
if (key != null) {
(keyed ??= {})[key] = child;
} else {
deactivateChild(child);
}
}
for (; newTop <= newBottom; newTop++) {
final widget = newWidgets[newTop];
var child = keyed?[widget.key];
if (child != null &&
!_forgotten(forgottenChildren, child) &&
Widget.canUpdate(child.widget, widget)) {
keyed!.remove(widget.key);
} else {
// one with the key but of another type is removed with those left,
// and one forgotten is taken back from where it went
child = null;
}
previous = updateChild(child, widget, _slotAt(child, newTop, previous))!;
if (inPlace) {
newChildren[newTop] = previous;
} else {
newChildren.add(previous);
}
}
for (var i = oldBottom + 1; i < oldChildren.length; i++, newTop++) {
final child = oldChildren[i];
if (_forgotten(forgottenChildren, child)) {
// taken back from where it went
previous = updateChild(
null,
newWidgets[newTop],
IndexedSlot(newTop, previous),
)!;
} else {
previous = updateChild(
child,
newWidgets[newTop],
_slotAt(child, newTop, previous),
)!;
}
if (inPlace) {
newChildren[newTop] = previous;
} else {
newChildren.add(previous);
}
}
if (keyed != null) {
for (final child in keyed.values) {
if (!_forgotten(forgottenChildren, child)) {
deactivateChild(child);
}
}
}
return newChildren;
}