text stringlengths 1 474 |
|---|
/// Shortcuts defined here are in effect for the whole app, |
/// although different widgets may fulfill them differently. |
class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
static const String title = 'Shortcuts and Actions Demo'; |
@override |
Widget build(BuildContext context) { |
return MaterialApp( |
title: title, |
theme: ThemeData( |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
), |
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 |
issue if you are using those.<topic_end> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.