text
stringlengths
1
474
onKeyEvent: (node, event) {
return (event.logicalKey == LogicalKeyboardKey.keyA)
? KeyEventResult.handled
: KeyEventResult.ignored;
},
child: const TextField(),
);
}<code_end>
If the intent is input validation, this example’s functionality would probably
be better implemented using a TextInputFormatter, but the technique can still
be useful: the Shortcuts widget uses this method to handle shortcuts before
they become text input, for instance.<topic_end>
<topic_start>
Controlling what gets focus
One of the main aspects of focus is controlling what can receive focus and how.
The attributes canRequestFocus, skipTraversal, and descendantsAreFocusable
control how this node and its descendants participate in the focus process.If the skipTraversal attribute true, then this focus node doesn’t participate
in focus traversal. It is still focusable if requestFocus is called on its
focus node, but is otherwise skipped when the focus traversal system is looking
for the next thing to focus on.The canRequestFocus attribute, unsurprisingly, controls whether or not the
focus node that this Focus widget manages can be used to request focus. If
this attribute is false, then calling requestFocus on the node has no effect.
It also implies that this node is skipped for focus traversal, since it can’t
request focus.The descendantsAreFocusable attribute controls whether the descendants of this
node can receive focus, but still allows this node to receive focus. This
attribute can be used to turn off focusability for an entire widget subtree.
This is how the ExcludeFocus widget works: it’s just a Focus widget with
this attribute set.<topic_end>
<topic_start>
Autofocus
Setting the autofocus attribute of a Focus widget tells the widget to
request the focus the first time the focus scope it belongs to is focused. If
more than one widget has autofocus set, then it is arbitrary which one
receives the focus, so try to only set it on one widget per focus scope.The autofocus attribute only takes effect if there isn’t already a focus in
the scope that the node belongs to.Setting the autofocus attribute on two nodes that belong to different focus
scopes is well defined: each one becomes the focused widget when their
corresponding scopes are focused.<topic_end>
<topic_start>
Change notifications
The Focus.onFocusChanged callback can be used to get notifications that the
focus state for a particular node has changed. It notifies if the node is added
to or removed from the focus chain, which means it gets notifications even if it
isn’t the primary focus. If you only want to know if you have received the
primary focus, check and see if hasPrimaryFocus is true on the focus node.<topic_end>
<topic_start>
Obtaining the FocusNode
Sometimes, it is useful to obtain the focus node of a Focus widget to
interrogate its attributes.To access the focus node from an ancestor of the Focus widget, create and pass
in a FocusNode as the Focus widget’s focusNode attribute. Because it needs
to be disposed of, the focus node you pass needs to be owned by a stateful
widget, so don’t just create one each time it is built.If you need access to the focus node from the descendant of a Focus widget,
you can call Focus.of(context) to obtain the focus node of the nearest Focus
widget to the given context. If you need to obtain the FocusNode of a Focus
widget within the same build function, use a Builder to make sure you have
the correct context. This is shown in the following example:
<code_start>@override
Widget build(BuildContext context) {
return Focus(
child: Builder(
builder: (context) {
final bool hasPrimary = Focus.of(context).hasPrimaryFocus;
print('Building with primary focus: $hasPrimary');
return const SizedBox(width: 100, height: 100);
},
),
);
}<code_end>
<topic_end>
<topic_start>
Timing
One of the details of the focus system is that when focus is requested, it only
takes effect after the current build phase completes. This means that focus
changes are always delayed by one frame, because changing focus can
cause arbitrary parts of the widget tree to rebuild, including ancestors of the
widget currently requesting focus. Because descendants cannot dirty their
ancestors, it has to happen between frames, so that any needed changes can
happen on the next frame.<topic_end>
<topic_start>
FocusScope widget
The FocusScope widget is a special version of the Focus widget that manages
a FocusScopeNode instead of a FocusNode. The FocusScopeNode is a special
node in the focus tree that serves as a grouping mechanism for the focus nodes
in a subtree. Focus traversal stays within a focus scope unless a node outside
of the scope is explicitly focused.The focus scope also keeps track of the current focus and history of the nodes
focused within its subtree. That way, if a node releases focus or is removed
when it had focus, the focus can be returned to the node that had focus
previously.Focus scopes also serve as a place to return focus to if none of the descendants
have focus. This allows the focus traversal code to have a starting context for
finding the next (or first) focusable control to move to.If you focus a focus scope node, it first attempts to focus the current, or most
recently focused node in its subtree, or the node in its subtree that requested
autofocus (if any). If there is no such node, it receives the focus itself.<topic_end>
<topic_start>
FocusableActionDetector widget
The FocusableActionDetector is a widget that combines the functionality of
Actions, Shortcuts, MouseRegion and a Focus widget to create
a detector that defines actions and key bindings, and provides callbacks for
handling focus and hover highlights. It is what Flutter controls use to
implement all of these aspects of the controls. It is just implemented using the
constituent widgets, so if you don’t need all of its functionality, you can just
use the ones you need, but it is a convenient way to build these behaviors into