text
stringlengths
1
372
canRequestFocus: false,
child: child,
);
}
<code_end>
focus key events are processed before text entry events, so handling a key event
when the focus widget surrounds a text field prevents that key from being
entered into the text field.
here’s an example of a widget that won’t allow the letter “a” to be typed into
the text field:
<code_start>
@override
widget build(BuildContext context) {
return focus(
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