Focus class

A widget that manages a FocusNode to allow keyboard focus to be given to this widget and its descendants.

When the focus is gained or lost, onFocusChanged is called.

For keyboard events, onKey is called if FocusNode.hasFocus is true for this widget's focusNode, unless a focused descendant's onKey callback returns false when called.

This widget does not provide any visual indication that the focus has changed. Any desired visual changes should be made when onFocusChanged is called.

To access the FocusNode of the nearest ancestor Focus widget and establish a relationship that will rebuild the widget when the focus changes, use the Focus.of and FocusScope.of static methods.

To access the focused state of the nearest Focus widget, use Focus.hasFocus from a build method, which also establishes a relationship between the calling widget and the Focus widget that will rebuild the calling widget when the focus changes.

Managing a FocusNode means managing its lifecycle, listening for changes in focus, and re-parenting it when needed to keep the focus hierarchy in sync with the widget hierarchy. This widget does all of those things for you. See FocusNode for more information about the details of what node management entails if you are not using a Focus widget and you need to do it yourself.

To collect a sub-tree of nodes into an exclusive group that restricts focus traversal to the group, use a FocusScope. To collect a sub-tree of nodes into a group that has a specific order to its traversal but allows the traversal to escape the group, use a FocusTraversalGroup.

To move the focus, use methods on FocusNode by getting the FocusNode through the of method. For instance, to move the focus to the next node in the focus traversal order, call Focus.of(context).nextFocus(). To unfocus a widget, call Focus.of(context).unfocus().

This example shows how to manage focus using the Focus and FocusScope widgets. See FocusNode for a similar example that doesn't use Focus or FocusScope.
To create a local project with this code sample, run:
flutter create --sample=widgets.Focus.1 mysample

This example shows how to wrap another widget in a Focus widget to make it focusable. It wraps a Container, and changes the container's color when it is set as the FocusManager.primaryFocus.

If you also want to handle mouse hover and/or keyboard actions on a widget, consider using a FocusableActionDetector, which combines several different widgets to provide those capabilities.

To create a local project with this code sample, run:
flutter create --sample=widgets.Focus.2 mysample

This example shows how to focus a newly-created widget immediately after it is created.

The focus node will not actually be given the focus until after the frame in which it has requested focus is drawn, so it is OK to call FocusNode.requestFocus on a node which is not yet in the focus tree.

To create a local project with this code sample, run:
flutter create --sample=widgets.Focus.3 mysample

See also:

  • FocusNode, which represents a node in the focus hierarchy and FocusNode's API documentation includes a detailed explanation of its role in the overall focus system.
  • FocusScope, a widget that manages a group of focusable widgets using a FocusScopeNode.
  • FocusScopeNode, a node that collects focus nodes into a group for traversal.
  • FocusManager, a singleton that manages the primary focus and distributes key events to focused nodes.
  • FocusTraversalPolicy, an object used to determine how to move the focus to other nodes.
  • FocusTraversalGroup, a widget that groups together and imposes a traversal policy on the Focus nodes below it in the widget hierarchy.


Focus({Key key, @required Widget child, FocusNode focusNode, bool autofocus: false, ValueChanged<bool> onFocusChange, FocusOnKeyCallback onKey, String debugLabel, bool canRequestFocus, bool skipTraversal, bool includeSemantics: true})
Creates a widget that manages a FocusNode. [...]


autofocus bool
True if this widget will be selected as the initial focus when no other node in its scope is currently focused. [...]
canRequestFocus bool
If true, this widget may request the primary focus. [...]
child Widget
The child widget of this Focus. [...]
debugLabel String
A debug label for this widget. [...]
focusNode FocusNode
An optional focus node to use as the focus node for this widget. [...]
includeSemantics bool
Include semantics information in this Focus widget. [...]
onFocusChange ValueChanged<bool>
Handler called when the focus changes. [...]
onKey FocusOnKeyCallback
Handler for keys pressed when this object or one of its children has focus. [...]
skipTraversal bool
Sets the FocusNode.skipTraversal flag on the focus node so that it won't be visited by the FocusTraversalPolicy. [...]
hashCode int
The hash code for this object. [...]
@nonVirtual, read-only, inherited
key Key
Controls how one widget replaces another widget in the tree. [...]
final, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited


createState() → _FocusState
Creates the mutable state for this widget at a given location in the tree. [...]
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node. [...]
createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree. [...]
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
@protected, inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
toDiagnosticsNode({String name, DiagnosticsTreeStyle style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
toString({DiagnosticLevel minLevel:}) String
Returns a string representation of this object.
toStringDeep({String prefixLineOne: '', String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
toStringShallow({String joiner: ', ', DiagnosticLevel minLevel: DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
toStringShort() String
A short, textual description of this widget.


operator ==(Object other) bool
The equality operator. [...]
@nonVirtual, inherited

Static Methods

isAt(BuildContext context) bool
Returns true if the nearest enclosing Focus widget's node is focused. [...]
of(BuildContext context, {bool nullOk: false, bool scopeOk: false}) FocusNode
Returns the focusNode of the Focus that most tightly encloses the given BuildContext. [...]