FocusNode class
A leaf node in the focus tree that can receive focus.
The focus tree keeps track of which widget is the user's current focus. The focused widget often listens for keyboard events.
To request focus, find the FocusScopeNode for the current BuildContext and call the FocusScopeNode.requestFocus method:
FocusScope.of(context).requestFocus(focusNode);
If your widget requests focus, be sure to call
FocusScope.of(context).reparentIfNeeded(focusNode);
in your build
method to reparent your FocusNode if your widget moves from one
location in the tree to another.
Lifetime
Focus nodes are long-lived objects. For example, if a stateful widget has a focusable child widget, it should create a FocusNode in the State.initState method, and dispose it in the State.dispose method, providing the same FocusNode to the focusable child each time the State.build method is run. In particular, creating a FocusNode each time State.build is invoked will cause the focus to be lost each time the widget is built.
See also:
- FocusScopeNode, which is an interior node in the focus tree.
- FocusScope.of, which provides the FocusScopeNode for a given BuildContext.
- Inheritance
- Object
- ChangeNotifier
- FocusNode
Constructors
Properties
- hasFocus → bool
-
Whether this node has the overall focus. [...]
read-only
- hashCode → int
-
The hash code for this object. [...]
read-only, inherited
- hasListeners → bool
-
Whether any listeners are currently registered. [...]
@protected, read-only, inherited
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
Methods
-
consumeKeyboardToken(
) → bool - Removes the keyboard token from this focus node if it has one. [...]
-
dispose(
) → void -
Discards any resources used by the object. After this is called, the
object is not in a usable state and should be discarded (calls to
addListener and removeListener will throw after the object is
disposed). [...]
override
-
toString(
) → String -
Returns a string representation of this object.
override
-
unfocus(
) → void - Cancels any outstanding requests for focus. [...]
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes. [...]
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed. [...]
inherited
-
notifyListeners(
) → void -
Call all the registered listeners. [...]
@protected, inherited
-
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes. [...]
inherited
Operators
-
operator ==(
dynamic other) → bool -
The equality operator. [...]
inherited