text stringlengths 1 474 |
|---|
<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, |
canRequestFocus: false, |
child: child, |
); |
}<code_end> |
Focus key events are processed before text entry events, so handling a key event |
when the focus widget surrounds a text field prevents that key from being |
entered into the text field.Here’s an example of a widget that won’t allow the letter “a” to be typed into |
the text field: |
<code_start>@override |
Widget build(BuildContext context) { |
return Focus( |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.