inDirection method

  1. @mustCallSuper
  2. @override
bool inDirection(
  1. FocusNode currentNode,
  2. TraversalDirection direction
)
override

Focuses the next widget in the given direction in the FocusScope that contains the currentNode.

This determines what the next node to receive focus in the given direction will be by inspecting the node tree, and then calling FocusNode.requestFocus on it.

Returns true if it successfully found a node and requested focus.

Maintains a stack of previous locations that have been visited on the policy data for the affected FocusScopeNode. If the previous direction was the opposite of the current direction, then the this policy will request focus on the previously focused node. Change to another direction other than the current one or its opposite will clear the stack.

If this function returns true when called by a subclass, then the subclass should return true and not request focus from any node.

Implementation

@mustCallSuper
@override
bool inDirection(FocusNode currentNode, TraversalDirection direction) {
  final FocusScopeNode nearestScope = currentNode.nearestScope!;
  final FocusNode? focusedChild = nearestScope.focusedChild;
  if (focusedChild == null) {
    final FocusNode firstFocus = findFirstFocusInDirection(currentNode, direction) ?? currentNode;
    switch (direction) {
      case TraversalDirection.up:
      case TraversalDirection.left:
        requestFocusCallback(
          firstFocus,
          alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
        );
      case TraversalDirection.right:
      case TraversalDirection.down:
        requestFocusCallback(
          firstFocus,
          alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
        );
    }
    return true;
  }
  if (_popPolicyDataIfNeeded(direction, nearestScope, focusedChild)) {
    return true;
  }
  final FocusNode? found = _findNextFocusInDirection(
    focusedChild,
    nearestScope.traversalDescendants,
    direction,
  );
  if (found != null) {
    _pushPolicyData(direction, nearestScope, focusedChild);
    return _requestTraversalFocusInDirection(currentNode, found, nearestScope, direction);
  }
  return _onEdgeForDirection(currentNode, focusedChild, direction);
}