reparent method Null safety

void reparent(
  1. {FocusNode? parent}
)

Ensures that the FocusNode attached at this attachment point has the proper parent node, changing it if necessary.

If given, ensures that the given parent node is the parent of the node that is attached at this attachment point, changing it if necessary. However, it is usually not necessary to supply an explicit parent, since reparent will use Focus.of to determine the correct parent node for the context given in FocusNode.attach.

If isAttached is false, then calling this method does nothing.

Should be called whenever the associated widget is rebuilt in order to maintain the focus hierarchy.

A StatefulWidget that hosts a FocusNode should call this method on the node it hosts during its State.build or State.didChangeDependencies methods in case the widget is moved from one location in the tree to another location that has a different FocusScope or context.

The optional parent argument must be supplied when not using Focus and FocusScope widgets to build the focus tree, or if there is a need to supply the parent explicitly (which are both uncommon).

Implementation

void reparent({FocusNode? parent}) {
  assert(_node != null);
  if (isAttached) {
    assert(_node.context != null);
    parent ??= Focus.maybeOf(_node.context!, scopeOk: true);
    parent ??= _node.context!.owner!.focusManager.rootScope;
    assert(parent != null);
    parent._reparent(_node);
  }
}