findFirstFocus method

FocusNode findFirstFocus (
  1. FocusNode currentNode

Returns the node that should receive focus if there is no current focus in the nearest FocusScopeNode that currentNode belongs to.

This is used by next/previous/inDirection to determine which node to focus if they are called when no node is currently focused.

The currentNode argument must not be null.

The default implementation returns the FocusScopeNode.focusedChild, if set, on the nearest scope of the currentNode, otherwise, returns the first node from sortDescendants, or the given currentNode if there are no descendants.


FocusNode findFirstFocus(FocusNode currentNode) {
  assert(currentNode != null);
  final FocusScopeNode scope = currentNode.nearestScope;
  FocusNode candidate = scope.focusedChild;
  if (candidate == null && scope.descendants.isNotEmpty) {
    final Iterable<FocusNode> sorted = _sortAllDescendants(scope);
    candidate = sorted.isNotEmpty ? sorted.first : null;

  // If we still didn't find any candidate, use the current node as a
  // fallback.
  candidate ??= currentNode;
  return candidate;