text stringlengths 1 372 |
|---|
issue if you are using those. |
<topic_end> |
<topic_start> |
focus widget |
the focus widget owns and manages a focus node, and is the workhorse of the |
focus system. it manages the attaching and detaching of the focus node it owns |
from the focus tree, manages the attributes and callbacks of the focus node, and |
has static functions to enable discovery of focus nodes attached to the widget |
tree. |
in its simplest form, wrapping the focus widget around a widget subtree allows |
that widget subtree to obtain focus as part of the focus traversal process, or |
whenever requestFocus is called on the FocusNode passed to it. when combined |
with a gesture detector that calls requestFocus, it can receive focus when |
tapped or clicked. |
you might pass a FocusNode object to the focus widget to manage, but if you |
don’t, it creates its own. the main reason to create your own |
FocusNode is to be able to call requestFocus() |
on the node to control the focus from a parent widget. most of the other |
functionality of a FocusNode is best accessed by changing the attributes of |
the focus widget itself. |
the focus widget is used in most of flutter’s own controls to implement their |
focus functionality. |
here is an example showing how to use the focus widget to make a custom |
control focusable. it creates a container with text that reacts to receiving the |
focus. |
<code_start> |
import 'package:flutter/material.dart'; |
void main() => runApp(const MyApp()); |
class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
static const string _title = 'focus sample'; |
@override |
widget build(BuildContext context) { |
return MaterialApp( |
title: _title, |
home: scaffold( |
appBar: AppBar(title: const text(_title)), |
body: const column( |
mainAxisAlignment: MainAxisAlignment.center, |
children: <widget>[mycustomwidget(), MyCustomWidget()], |
), |
), |
); |
} |
} |
class MyCustomWidget extends StatefulWidget { |
const MyCustomWidget({super.key}); |
@override |
State<MyCustomWidget> createState() => _MyCustomWidgetState(); |
} |
class _MyCustomWidgetState extends State<MyCustomWidget> { |
color _color = colors.white; |
string _label = 'unfocused'; |
@override |
widget build(BuildContext context) { |
return focus( |
onFocusChange: (focused) { |
setState(() { |
_color = focused ? colors.black26 : colors.white; |
_label = focused ? 'focused' : 'unfocused'; |
}); |
}, |
child: center( |
child: container( |
width: 300, |
height: 50, |
alignment: alignment.center, |
color: _color, |
child: text(_label), |
), |
), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
key events |
if you wish to listen for key events in a subtree, |
set the onKeyEvent attribute of the focus widget to |
be a handler that either just listens to the key, or |
handles the key and stops its propagation to other widgets. |
key events start at the focus node with primary focus. |
if that node doesn’t return KeyEventResult.handled from |
its onKeyEvent handler, then its parent focus node is given the event. |
if the parent doesn’t handle it, it goes to its parent, |
and so on, until it reaches the root of the focus tree. |
if the event reaches the root of the focus tree without being handled, then |
it is returned to the platform to give to |
the next native control in the application |
(in case the flutter UI is part of a larger native application UI). |
events that are handled are not propagated to other flutter widgets, |
and they are also not propagated to native widgets. |
here’s an example of a focus widget that absorbs every key that |
its subtree doesn’t handle, without being able to be the primary focus: |
<code_start> |
@override |
widget build(BuildContext context) { |
return focus( |
onKeyEvent: (node, event) => KeyEventResult.handled, |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.