canRequestFocus property Null safety

bool canRequestFocus

If true, this focus node may request the primary focus.

Defaults to true. Set to false if you want this node to do nothing when requestFocus is called on it.

If set to false on a FocusScopeNode, will cause all of the children of the scope node to not be focusable.

If set to false on a FocusNode, it will not affect the focusability of children of the node.

The hasFocus member can still return true if this node is the ancestor of a node with primary focus.

This is different than skipTraversal because skipTraversal still allows the node to be focused, just not traversed to via the FocusTraversalPolicy.

Setting canRequestFocus to false implies that the node will also be skipped for traversal purposes.

See also:

  • FocusTraversalGroup, a widget used to group together and configure the focus traversal policy for a widget subtree.
  • FocusTraversalPolicy, a class that can be extended to describe a traversal policy.

Implementation

bool get canRequestFocus {
  if (!_canRequestFocus) {
    return false;
  }
  final FocusScopeNode? scope = enclosingScope;
  if (scope != null && !scope.canRequestFocus) {
    return false;
  }
  for (final FocusNode ancestor in ancestors) {
    if (!ancestor.descendantsAreFocusable) {
      return false;
    }
  }
  return true;
}
void canRequestFocus=(bool value)
mustCallSuper">@mustCallSuper

Implementation

@mustCallSuper
set canRequestFocus(bool value) {
  if (value != _canRequestFocus) {
    // Have to set this first before unfocusing, since it checks this to cull
    // unfocusable, previously-focused children.
    _canRequestFocus = value;
    if (hasFocus && !value) {
      unfocus(disposition: UnfocusDisposition.previouslyFocusedChild);
    }
    _manager?._markPropertiesChanged(this);
  }
}