autofocus method

void autofocus(
  1. FocusNode node
)

If this scope lacks a focus, request that the given node become the focus.

If the given node is not yet part of the focus tree, then add it as a child of this node.

Useful for widgets that wish to grab the focus if no other widget already has the focus.

The node is notified that it has received the primary focus in a microtask, so notification may lag the request by up to one frame.

Implementation

void autofocus(FocusNode node) {
  // Attach the node to the tree first, so in _applyFocusChange if the node
  // is detached we don't add it back to the tree.
  if (node._parent == null) {
    _reparent(node);
  }

  assert(_manager != null);
  assert(_focusDebug(() => 'Autofocus scheduled for $node: scope $this'));
  _manager?._pendingAutofocuses.add(_Autofocus(scope: this, autofocusNode: node));
  _manager?._markNeedsUpdate();
}