setPrimaryFocus method

void setPrimaryFocus(
  1. FocusNode? node
)

Sets focus to node, updating the focus path to the root.

Implementation

void setPrimaryFocus(FocusNode? node) {
  if (_primaryFocus == node) return;

  final oldPath = _getPathToRoot(_primaryFocus);
  final newPath = _getPathToRoot(node);

  // Unfocus nodes no longer on the active path
  for (final oldNode in oldPath) {
    if (!newPath.contains(oldNode)) {
      oldNode._setFocused(false);
    }
  }

  // Focus nodes entering the active path
  for (final newNode in newPath) {
    if (!oldPath.contains(newNode)) {
      newNode._setFocused(true);
    }
    if (newNode.parent is FocusScopeNode) {
      (newNode.parent as FocusScopeNode)._focusedChild = newNode;
    }
  }

  _primaryFocus = node;
}