@@ -34,8 +34,8 @@ TextField(
3434);
3535```
3636
37- For more information on handling input and creating text fields,
38- see the [ Forms] [ ] section of the cookbook.
37+ To learn more about handling input and creating text fields,
38+ consult the [ Forms] [ ] section of the cookbook.
3939
4040## Focus a text field when a button is tapped
4141
@@ -46,9 +46,9 @@ text field in response to an API call or a validation error.
4646In this example, give focus to a text field after the user
4747presses a button using the following steps:
4848
49- 1 . Create a ` FocusNode ` .
50- 2 . Pass the ` FocusNode ` to a ` TextField ` .
51- 3 . Give focus to the ` TextField ` when a button is tapped.
49+ 1 . Create a ` FocusNode ` .
50+ 1 . Pass the ` FocusNode ` to a ` TextField ` .
51+ 1 . Give focus to the ` TextField ` when a button is tapped.
5252
5353### 1. Create a ` FocusNode `
5454
@@ -77,7 +77,7 @@ class MyCustomForm extends StatefulWidget {
7777class _MyCustomFormState extends State<MyCustomForm> {
7878 // Define the focus node. To manage the lifecycle, create the FocusNode in
7979 // the initState method, and clean it up in the dispose method.
80- late FocusNode myFocusNode;
80+ late final FocusNode myFocusNode;
8181
8282 @override
8383 void initState() {
@@ -126,6 +126,8 @@ FloatingActionButton(
126126 // When the button is pressed,
127127 // give focus to the text field using myFocusNode.
128128 onPressed: () => myFocusNode.requestFocus(),
129+ tooltip: 'Focus Second Text Field',
130+ child: const Icon(Icons.edit),
129131),
130132```
131133
@@ -159,7 +161,7 @@ class MyCustomForm extends StatefulWidget {
159161class _MyCustomFormState extends State<MyCustomForm> {
160162 // Define the focus node. To manage the lifecycle, create the FocusNode in
161163 // the initState method, and clean it up in the dispose method.
162- late FocusNode myFocusNode;
164+ late final FocusNode myFocusNode;
163165
164166 @override
165167 void initState() {
@@ -198,21 +200,17 @@ class _MyCustomFormState extends State<MyCustomForm> {
198200 onPressed: () => myFocusNode.requestFocus(),
199201 tooltip: 'Focus Second Text Field',
200202 child: const Icon(Icons.edit),
201- ), // This trailing comma makes auto-formatting nicer for build methods.
203+ ),
202204 );
203205 }
204206}
205207```
206208
207209<noscript >
208- <img src =" /assets/images/docs/cookbook/focus.webp " alt =" Text Field Focus Demo " class = " site-mobile-screenshot " />
210+ <img src =" /assets/images/docs/cookbook/focus.png " alt =" Text Field Focus Demo " />
209211</noscript >
210212
211213
212- [ fix has landed ] : {{site.repo.flutter}}/pull/50372
213214[ `FocusNode` ] : {{site.api}}/flutter/widgets/FocusNode-class.html
214215[ Forms ] : /cookbook/forms
215- [ flutter/flutter@bf551a3 ] : {{site.repo.flutter}}/commit/bf551a31fe7ef45c854a219686b6837400bfd94c
216- [ Issue 52221 ] : {{site.repo.flutter}}/issues/52221
217216[ `requestFocus()` ] : {{site.api}}/flutter/widgets/FocusNode/requestFocus.html
218- [ workaround ] : {{site.repo.flutter}}/issues/52221#issuecomment-598244655
0 commit comments