text stringlengths 1 372 |
|---|
return const MaterialApp( |
title: 'text field focus', |
home: MyCustomForm(), |
); |
} |
} |
// 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) { |
return scaffold( |
appBar: AppBar( |
title: const Text('Text field focus'), |
), |
body: padding( |
padding: const EdgeInsets.all(16), |
child: column( |
children: [ |
// the first text field is focused on as soon as the app starts. |
const TextField( |
autofocus: true, |
), |
// the second text field is focused on when a user taps the |
// FloatingActionButton. |
TextField( |
focusNode: myFocusNode, |
), |
], |
), |
), |
floatingActionButton: FloatingActionButton( |
// when the button is pressed, |
// give focus to the text field using myFocusNode. |
onPressed: () => myFocusNode.requestFocus(), |
tooltip: 'focus second text field', |
child: const Icon(Icons.edit), |
), // this trailing comma makes auto-formatting nicer for build methods. |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
build a form with validation |
apps often require users to enter information into a text field. |
for example, you might require users to log in with an email address |
and password combination. |
to make apps secure and easy to use, check whether the |
information the user has provided is valid. if the user has correctly filled |
out the form, process the information. if the user submits incorrect |
information, display a friendly error message letting them know what went |
wrong. |
in this example, learn how to add validation to a form that has |
a single text field using the following steps: |
<topic_end> |
<topic_start> |
1. create a form with a GlobalKey |
create a form. |
the form widget acts as a container for grouping and |
validating multiple form fields. |
when creating the form, provide a GlobalKey. |
this assigns a unique identifier to your form. |
it also allows you to validate the form later. |
create the form as a StatefulWidget. |
this allows you to create a unique GlobalKey<FormState>() once. |
you can then store it as a variable and access it at different points. |
if you made this a StatelessWidget, you’d need to store this key somewhere. |
as it is resource expensive, you wouldn’t want to generate a new |
GlobalKey each time you run the build method. |
<code_start> |
import 'package:flutter/material.dart'; |
// define a custom form widget. |
class MyCustomForm extends StatefulWidget { |
const MyCustomForm({super.key}); |
@override |
MyCustomFormState createState() { |
return MyCustomFormState(); |
} |
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.