Focus class Null safety
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, onFocusChange is called.
For keyboard events, onKey and onKeyEvent are called if FocusNode.hasFocus is true for this widget's focusNode, unless a focused descendant's onKey or onKeyEvent callback returned KeyEventResult.handled when called.
This widget does not provide any visual indication that the focus has changed. Any desired visual changes should be made when onFocusChange 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 FocusNode.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.
If the Focus default constructor is used, then this widget will manage any given focusNode by overwriting the appropriate values of the focusNode with the values of FocusNode.onKey, FocusNode.onKeyEvent, FocusNode.skipTraversal, FocusNode.canRequestFocus, and FocusNode.descendantsAreFocusable whenever the Focus widget is updated.
If the Focus.withExternalFocusNode is used instead, then the values returned by onKey, onKeyEvent, skipTraversal, canRequestFocus, and descendantsAreFocusable will be the values in the external focus node, and the external focus node's values will not be overwritten when the widget is updated.
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()
.
flutter create --sample=widgets.Focus.1 mysample
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.
flutter create --sample=widgets.Focus.2 mysample
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.
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.
- Inheritance
- Implementers
Constructors
-
Focus({Key? key, required Widget child, FocusNode? focusNode, bool autofocus = false, ValueChanged<
bool> ? onFocusChange, FocusOnKeyEventCallback? onKeyEvent, FocusOnKeyCallback? onKey, bool? canRequestFocus, bool? skipTraversal, bool? descendantsAreFocusable, bool? descendantsAreTraversable, bool includeSemantics = true, String? debugLabel}) -
Creates a widget that manages a FocusNode.
const
-
Focus.withExternalFocusNode({Key? key, required Widget child, required FocusNode focusNode, bool autofocus, ValueChanged<
bool> ? onFocusChange, bool includeSemantics}) -
Creates a Focus widget that uses the given
focusNode
as the source of truth for attributes on the node, rather than the attributes of this widget.constfactory
Properties
- autofocus → bool
-
True if this widget will be selected as the initial focus when no other
node in its scope is currently focused.
final
- canRequestFocus → bool
-
If true, this widget may request the primary focus.
read-only
- child → Widget
-
The child widget of this Focus.
final
- debugLabel → String?
-
A debug label for this widget.
read-only
- descendantsAreFocusable → bool
-
If false, will make this widget's descendants unfocusable.
read-only
- descendantsAreTraversable → bool
-
If false, will make this widget's descendants untraversable.
read-only
- focusNode → FocusNode?
-
An optional focus node to use as the focus node for this widget.
final
- hashCode → int
- The hash code for this object.
- includeSemantics → bool
-
Include semantics information in this widget.
final
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
-
onFocusChange
→ ValueChanged<
bool> ? -
Handler called when the focus changes.
final
- onKey → FocusOnKeyCallback?
-
A handler for keys that are pressed when this object or one of its
children has focus.
read-only
- onKeyEvent → FocusOnKeyEventCallback?
-
A handler for keys that are pressed when this object or one of its
children has focus.
read-only
- runtimeType → Type
-
A representation of the runtime type of the object.
read-onlyinherited
- skipTraversal → bool
-
Sets the FocusNode.skipTraversal flag on the focus node so that it won't
be visited by the FocusTraversalPolicy.
read-only
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → State< Focus> -
Creates the mutable state for this widget at a given location in the tree.
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of
DiagnosticsNode
objects describing this node's children.protected">@protectedinherited -
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
nonVirtual">@nonVirtualinherited
Static Methods
-
isAt(
BuildContext context) → bool - Returns true if the nearest enclosing Focus widget's node is focused.
-
maybeOf(
BuildContext context, {bool scopeOk = false}) → FocusNode? - Returns the focusNode of the Focus that most tightly encloses the given BuildContext.
-
of(
BuildContext context, {bool scopeOk = false}) → FocusNode - Returns the focusNode of the Focus that most tightly encloses the given BuildContext.