text
stringlengths
1
372
home: shortcuts(
shortcuts: <logicalkeyset, intent>{
LogicalKeySet(LogicalKeyboardKey.escape): const ClearIntent(),
LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyC):
const CopyIntent(),
LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyA):
const SelectAllIntent(),
},
child: const CopyableTextField(title: title),
),
);
}
}
void main() => runApp(const MyApp());
<code_end>
<topic_end>
<topic_start>
understanding flutter's keyboard focus system
this article explains how to control where keyboard input is directed. if you
are implementing an application that uses a physical keyboard, such as most
desktop and web applications, this page is for you. if your app won’t be used
with a physical keyboard, you can skip this.
<topic_end>
<topic_start>
overview
flutter comes with a focus system that directs the keyboard input to a
particular part of an application. in order to do this, users “focus” the input
onto that part of an application by tapping or clicking the desired UI element.
once that happens, text entered with the keyboard flows to that part of the
application until the focus moves to another part of the application. focus can
also be moved by pressing a particular keyboard shortcut, which is typically
bound to tab, so it is sometimes called “tab traversal”.
this page explores the APIs used to perform these operations on a flutter
application, and how the focus system works. we have noticed that there is some
confusion among developers about how to define and use FocusNode objects.
if that describes your experience, skip ahead to the best practices for
creating FocusNode objects.
<topic_end>
<topic_start>
focus use cases
some examples of situations where you might need to know how to use the focus
system:
<topic_end>
<topic_start>
glossary
below are terms, as flutter uses them, for elements of the focus system. the
various classes that implement some of these concepts are introduced below.
<topic_end>
<topic_start>
FocusNode and FocusScopeNode
the FocusNode and FocusScopeNode objects implement the
mechanics of the focus system. they are long-lived objects (longer than widgets,
similar to render objects) that hold the focus state and attributes so that they
are persistent between builds of the widget tree. together, they form
the focus tree data structure.
they were originally intended to be developer-facing objects used to control
some aspects of the focus system, but over time they have evolved to mostly
implement details of the focus system. in order to prevent breaking existing
applications, they still contain public interfaces for their attributes. but, in
general, the thing for which they are most useful is to act as a relatively
opaque handle, passed to a descendant widget in order to call requestFocus()
on an ancestor widget, which requests that a descendant widget obtain focus.
setting of the other attributes is best managed by a focus or
FocusScope widget, unless you are not using them, or implementing your own
version of them.
<topic_end>
<topic_start>
best practices for creating FocusNode objects
some dos and don’ts around using these objects include:
<topic_end>
<topic_start>
unfocusing
there is an API for telling a node to “give up the focus”, named
FocusNode.unfocus(). while it does remove focus from the node, it is important
to realize that there really is no such thing as “unfocusing” all nodes. if a
node is unfocused, then it must pass the focus somewhere else, since there is
always a primary focus. the node that receives the focus when a node calls
unfocus() is either the nearest FocusScopeNode, or a previously focused node
in that scope, depending upon the disposition argument given to unfocus().
if you would like more control over where the focus goes when you remove it from
a node, explicitly focus another node instead of calling unfocus(), or use the
focus traversal mechanism to find another node with the focusInDirection,
nextFocus, or previousFocus methods on FocusNode.
when calling unfocus(), the disposition argument allows two modes for
unfocusing: UnfocusDisposition.scope and
UnfocusDisposition.previouslyFocusedChild. the default is scope, which gives
the focus to the nearest parent focus scope. this means that if the focus is
thereafter moved to the next node with FocusNode.nextFocus, it starts with the
“first” focusable item in the scope.
the previouslyFocusedChild disposition will search the scope to find the
previously focused child and request focus on it. if there is no previously
focused child, it is equivalent to scope.
beware: if there is no other scope, then focus moves to the root scope node of
the focus system, FocusManager.rootScope. this is generally not desirable, as
the root scope doesn’t have a context for the framework to determine which
node should be focused next. if you find that your application suddenly loses
the ability to navigate by using focus traversal, this is probably what has
happened. to fix it, add a FocusScope as an ancestor to the focus node that
is requesting the unfocus. the WidgetsApp (from which MaterialApp and
CupertinoApp are derived) has its own FocusScope, so this should not be an