text
stringlengths
1
372
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
your custom controls.
info note
to learn more, watch this short widget of the week video on the FocusableActionDetector widget:
<topic_end>
<topic_start>
controlling focus traversal
once an application has the ability to focus, the next thing many apps want to
do is to allow the user to control the focus using the keyboard or another input
device. the most common example of this is “tab traversal” where the user
presses tab to go to the “next” control. controlling what “next”
means is the subject of this section. this kind of traversal is provided by
flutter by default.
in a simple grid layout, it’s fairly easy to decide which control is next. if
you’re not at the end of the row, then it’s the one to the right (or left for
right-to-left locales). if you are at the end of a row, it’s the first control
in the next row. unfortunately, applications are rarely laid out in grids, so
more guidance is often needed.
the default algorithm in flutter (readingordertraversalpolicy) for focus
traversal is pretty good: it gives the right answer for most applications.
however, there are always pathological cases, or cases where the context or
design requires a different order than the one the default ordering algorithm
arrives at. for those cases, there are other mechanisms for achieving the
desired order.
<topic_end>
<topic_start>
FocusTraversalGroup widget
the FocusTraversalGroup widget should be placed in the tree around widget
subtrees that should be fully traversed before moving on to another widget or
group of widgets. just grouping widgets into related groups is often enough to
resolve many tab traversal ordering problems. if not, the group can also be
given a FocusTraversalPolicy to determine the ordering within the group.
the default ReadingOrderTraversalPolicy is usually sufficient, but in
cases where more control over ordering is needed, an
OrderedTraversalPolicy can be used. the order argument of the
FocusTraversalOrder widget wrapped around the focusable components
determines the order. the order can be any subclass of FocusOrder, but
NumericFocusOrder and LexicalFocusOrder are provided.
if none of the provided focus traversal policies are sufficient for your
application, you could also write your own policy and use it to determine any
custom ordering you want.
here’s an example of how to use the FocusTraversalOrder widget to traverse a
row of buttons in the order TWO, ONE, THREE using NumericFocusOrder.
<code_start>
class OrderedButtonRow extends StatelessWidget {
const OrderedButtonRow({super.key});
@override
widget build(BuildContext context) {
return FocusTraversalGroup(
policy: OrderedTraversalPolicy(),
child: row(
children: <widget>[
const spacer(),
FocusTraversalOrder(
order: const NumericFocusOrder(2),
child: TextButton(
child: const Text('ONE'),
onPressed: () {},
),
),
const spacer(),
FocusTraversalOrder(
order: const NumericFocusOrder(1),
child: TextButton(
child: const Text('TWO'),
onPressed: () {},
),
),