updateChild method
Update the given child with the given new configuration.
This method is the core of the components system. It is called each time we are to add, update, or remove a child based on an updated configuration.
If the child
is null, and the newComponent
is not null, then we have a new
child for which we need to create an Element, configured with newComponent
.
If the newComponent
is null, and the child
is not null, then we need to
remove it because it no longer has a configuration.
If neither are null, then we need to update the child
's configuration to
be the new configuration given by newComponent
. If newComponent
can be given
to the existing child (as determined by Component.canUpdate), then it is so
given. Otherwise, the old child needs to be disposed and a new child
created for the new configuration.
If both are null, then we don't have a child and won't have a child, so we do nothing.
The updateChild method returns the new child, if it had to create one, or the child that was passed in, if it just had to update the child, or null, if it removed the child and did not replace it.
The following table summarizes the above:
newComponent == null | newComponent != null | |
---|---|---|
child == null | Returns null. | Returns new Element. |
child != null | Old child is removed, returns null. | Old child updated if possible, returns child or new Element. |
Implementation
@protected
Element? updateChild(Element? child, Component? newComponent, Element? prevSibling) {
if (newComponent == null) {
if (child != null) {
if (_lastChild == child) {
updateLastChild(prevSibling);
}
deactivateChild(child);
}
return null;
}
final Element newChild;
if (child != null) {
if (child._component == newComponent) {
if (child._parentChanged || child._prevSibling != prevSibling) {
child.updatePrevSibling(prevSibling);
}
newChild = child;
} else if (child._parentChanged || Component.canUpdate(child.component, newComponent)) {
if (child._parentChanged || child._prevSibling != prevSibling) {
child.updatePrevSibling(prevSibling);
}
var oldComponent = child.component;
child.update(newComponent);
assert(child.component == newComponent);
child.didUpdate(oldComponent);
newChild = child;
} else {
deactivateChild(child);
assert(child._parent == null);
newChild = inflateComponent(newComponent, prevSibling);
}
} else {
newChild = inflateComponent(newComponent, prevSibling);
}
if (_lastChild == prevSibling) {
updateLastChild(newChild);
}
return newChild;
}