text stringlengths 1 474 |
|---|
Widget build(BuildContext context) { |
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(); |
} |
} |
// Define a corresponding State class. |
// This class holds data related to the form. |
class MyCustomFormState extends State<MyCustomForm> { |
// Create a global key that uniquely identifies the Form widget |
// and allows validation of the form. |
// |
// Note: This is a `GlobalKey<FormState>`, |
// not a GlobalKey<MyCustomFormState>. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.