Skip to content

Commit f859352

Browse files
committed
Refreshing the forms pages in the cookbook
1 parent 0b7f205 commit f859352

22 files changed

Lines changed: 92 additions & 83 deletions

File tree

‎examples/cookbook/forms/focus/lib/main.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MyCustomForm extends StatefulWidget {
2424
class _MyCustomFormState extends State<MyCustomForm> {
2525
// Define the focus node. To manage the lifecycle, create the FocusNode in
2626
// the initState method, and clean it up in the dispose method.
27-
late FocusNode myFocusNode;
27+
late final FocusNode myFocusNode;
2828

2929
@override
3030
void initState() {
@@ -63,7 +63,7 @@ class _MyCustomFormState extends State<MyCustomForm> {
6363
onPressed: () => myFocusNode.requestFocus(),
6464
tooltip: 'Focus Second Text Field',
6565
child: const Icon(Icons.edit),
66-
), // This trailing comma makes auto-formatting nicer for build methods.
66+
),
6767
);
6868
}
6969
}

‎examples/cookbook/forms/focus/lib/starter.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MyCustomForm extends StatefulWidget {
1414
class _MyCustomFormState extends State<MyCustomForm> {
1515
// Define the focus node. To manage the lifecycle, create the FocusNode in
1616
// the initState method, and clean it up in the dispose method.
17-
late FocusNode myFocusNode;
17+
late final FocusNode myFocusNode;
1818

1919
@override
2020
void initState() {

‎examples/cookbook/forms/focus/lib/step2.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MyCustomForm extends StatefulWidget {
1313
class _MyCustomFormState extends State<MyCustomForm> {
1414
// Define the focus node. To manage the lifecycle, create the FocusNode in
1515
// the initState method, and clean it up in the dispose method.
16-
late FocusNode myFocusNode;
16+
late final FocusNode myFocusNode;
1717

1818
@override
1919
void initState() {

‎examples/cookbook/forms/focus/lib/step3.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MyCustomForm extends StatefulWidget {
1313
class _MyCustomFormState extends State<MyCustomForm> {
1414
// Define the focus node. To manage the lifecycle, create the FocusNode in
1515
// the initState method, and clean it up in the dispose method.
16-
late FocusNode myFocusNode;
16+
late final FocusNode myFocusNode;
1717

1818
@override
1919
void initState() {
@@ -51,6 +51,8 @@ class _MyCustomFormState extends State<MyCustomForm> {
5151
// When the button is pressed,
5252
// give focus to the text field using myFocusNode.
5353
onPressed: () => myFocusNode.requestFocus(),
54+
tooltip: 'Focus Second Text Field',
55+
child: const Icon(Icons.edit),
5456
),
5557
// #enddocregion FloatingActionButton
5658
);

‎examples/cookbook/forms/retrieve_input/lib/main.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class _MyCustomFormState extends State<MyCustomForm> {
5252
context: context,
5353
builder: (context) {
5454
return AlertDialog(
55-
// Retrieve the text the that user has entered by using the
55+
// Retrieve the text that the user has entered by using the
5656
// TextEditingController.
5757
content: Text(myController.text),
5858
);

‎examples/cookbook/forms/text_field_changes/lib/main.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class MyApp extends StatelessWidget {
88
@override
99
Widget build(BuildContext context) {
1010
return const MaterialApp(
11-
title: 'Retrieve Text Input',
11+
title: 'Handle Text Field Changes',
1212
home: MyCustomForm(),
1313
);
1414
}
@@ -59,7 +59,7 @@ class _MyCustomFormState extends State<MyCustomForm> {
5959
@override
6060
Widget build(BuildContext context) {
6161
return Scaffold(
62-
appBar: AppBar(title: const Text('Retrieve Text Input')),
62+
appBar: AppBar(title: const Text('Handle Text Field Changes')),
6363
body: Padding(
6464
padding: const EdgeInsets.all(16),
6565
child: Column(

‎examples/cookbook/forms/text_input/lib/main.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MyCustomForm extends StatelessWidget {
2525
Widget build(BuildContext context) {
2626
return Column(
2727
crossAxisAlignment: CrossAxisAlignment.start,
28-
children: <Widget>[
28+
children: [
2929
const Padding(
3030
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 16),
3131
// #docregion TextField

‎examples/cookbook/forms/validation/lib/form.dart‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ class MyCustomForm extends StatefulWidget {
55
const MyCustomForm({super.key});
66

77
@override
8-
MyCustomFormState createState() {
9-
return MyCustomFormState();
10-
}
8+
State<MyCustomForm> createState() => _MyCustomFormState();
119
}
1210

1311
// Define a corresponding State class.
1412
// This class holds data related to the form.
15-
class MyCustomFormState extends State<MyCustomForm> {
13+
class _MyCustomFormState extends State<MyCustomForm> {
1614
// Create a global key that uniquely identifies the Form widget
1715
// and allows validation of the form.
1816
//
1917
// Note: This is a `GlobalKey<FormState>`,
20-
// not a GlobalKey<MyCustomFormState>.
18+
// not a GlobalKey<_MyCustomFormState>.
2119
final _formKey = GlobalKey<FormState>();
2220

2321
@override
@@ -26,7 +24,7 @@ class MyCustomFormState extends State<MyCustomForm> {
2624
return Form(
2725
key: _formKey,
2826
child: const Column(
29-
children: <Widget>[
27+
children: [
3028
// Add TextFormFields and ElevatedButton here.
3129
],
3230
),

‎examples/cookbook/forms/validation/lib/main.dart‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,17 @@ class MyCustomForm extends StatefulWidget {
2424
const MyCustomForm({super.key});
2525

2626
@override
27-
MyCustomFormState createState() {
28-
return MyCustomFormState();
29-
}
27+
State<MyCustomForm> createState() => _MyCustomFormState();
3028
}
3129

3230
// Create a corresponding State class.
3331
// This class holds data related to the form.
34-
class MyCustomFormState extends State<MyCustomForm> {
32+
class _MyCustomFormState extends State<MyCustomForm> {
3533
// Create a global key that uniquely identifies the Form widget
3634
// and allows validation of the form.
3735
//
3836
// Note: This is a GlobalKey<FormState>,
39-
// not a GlobalKey<MyCustomFormState>.
37+
// not a GlobalKey<_MyCustomFormState>.
4038
final _formKey = GlobalKey<FormState>();
4139

4240
@override

‎sites/docs/src/content/cookbook/forms/focus.md‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
4646
In this example, give focus to a text field after the user
4747
presses 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 {
7777
class _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 {
159161
class _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

Comments
 (0)