text
stringlengths
1
474
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: () {},
),
),
const Spacer(),
FocusTraversalOrder(
order: const NumericFocusOrder(3),
child: TextButton(
child: const Text('THREE'),
onPressed: () {},
),
),
const Spacer(),
],
),
);
}
}<code_end>
<topic_end>
<topic_start>
FocusTraversalPolicy
The FocusTraversalPolicy is the object that determines which widget is next,
given a request and the current focus node. The requests (member functions) are
things like findFirstFocus, findLastFocus, next, previous, and
inDirection.FocusTraversalPolicy is the abstract base class for concrete policies, like
ReadingOrderTraversalPolicy, OrderedTraversalPolicy and the
DirectionalFocusTraversalPolicyMixin classes.In order to use a FocusTraversalPolicy, you give one to a
FocusTraversalGroup, which determines the widget subtree in which the policy
will be effective. The member functions of the class are rarely called directly:
they are meant to be used by the focus system.<topic_end>
<topic_start>
The focus manager
The FocusManager maintains the current primary focus for the system. It
only has a few pieces of API that are useful to users of the focus system. One
is the FocusManager.instance.primaryFocus property, which contains the
currently focused focus node and is also accessible from the global
primaryFocus field.Other useful properties are FocusManager.instance.highlightMode and
FocusManager.instance.highlightStrategy. These are used by widgets that need
to switch between a “touch” mode and a “traditional” (mouse and keyboard) mode
for their focus highlights. When a user is using touch to navigate, the focus
highlight is usually hidden, and when they switch to a mouse or keyboard, the
focus highlight needs to be shown again so they know what is focused. The
hightlightStrategy tells the focus manager how to interpret changes in the
usage mode of the device: it can either automatically switch between the two
based on the most recent input events, or it can be locked in touch or
traditional modes. The provided widgets in Flutter already know how to use this