text
stringlengths
1
372
you can set the focus to the text field for the search term.
this allows the user to start typing as soon as the screen
is visible, without needing to manually tap the text field.
in this recipe, learn how to give the focus
to a text field as soon as it’s visible,
as well as how to give focus to a text field
when a button is tapped.
<topic_end>
<topic_start>
focus a text field as soon as it’s visible
to give focus to a text field as soon as it’s visible,
use the autofocus property.
for more information on handling input and creating text fields,
see the forms section of the cookbook.
<topic_end>
<topic_start>
focus a text field when a button is tapped
rather than immediately shifting focus to a specific text field,
you might need to give focus to a text field at a later point in time.
in the real world, you might also need to give focus to a specific
text field in response to an API call or a validation error.
in this example, give focus to a text field after the user
presses a button using the following steps:
<topic_end>
<topic_start>
1. create a FocusNode
first, create a FocusNode.
use the FocusNode to identify a specific TextField in flutter’s
“focus tree.” this allows you to give focus to the TextField
in the next steps.
since focus nodes are long-lived objects, manage the lifecycle
using a state object. use the following instructions to create
a FocusNode instance inside the initState() method of a
state class, and clean it up in the dispose() method:
<code_start>
// define a custom form widget.
class MyCustomForm extends StatefulWidget {
const MyCustomForm({super.key});
@override
State<MyCustomForm> createState() => _MyCustomFormState();
}
// define a corresponding state class.
// this class holds data related to the form.
class _MyCustomFormState extends State<MyCustomForm> {
// define the focus node. to manage the lifecycle, create the FocusNode in
// the initState method, and clean it up in the dispose method.
late FocusNode myFocusNode;
@override
void initState() {
super.initState();
myFocusNode = FocusNode();
}
@override
void dispose() {
// clean up the focus node when the form is disposed.
myFocusNode.dispose();
super.dispose();
}
@override
widget build(BuildContext context) {
// fill this out in the next step.
}
}
<code_end>
<topic_end>
<topic_start>
2. pass the FocusNode to a TextField
now that you have a FocusNode,
pass it to a specific TextField in the build() method.
<code_start>
@override
widget build(BuildContext context) {
return TextField(
focusNode: myFocusNode,
);
}
<code_end>
<topic_end>
<topic_start>
3. give focus to the TextField when a button is tapped
finally, focus the text field when the user taps a floating
action button. use the requestFocus() method to perform
this task.
<code_start>
FloatingActionButton(
// when the button is pressed,
// give focus to the text field using myFocusNode.
onPressed: () => myFocusNode.requestFocus(),
),
<code_end>
<topic_end>
<topic_start>
interactive example
<code_start>
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
widget build(BuildContext context) {