text stringlengths 1 474 |
|---|
final _formKey = GlobalKey<FormState>(); |
@override |
Widget build(BuildContext context) { |
// Build a Form widget using the _formKey created above. |
return Form( |
key: _formKey, |
child: const Column( |
children: <Widget>[ |
// Add TextFormFields and ElevatedButton here. |
], |
), |
); |
} |
}<code_end> |
lightbulb Tip |
Using a GlobalKey is the recommended way to access a form. |
However, if you have a more complex widget tree, |
you can use the Form.of() method to |
access the form within nested widgets.<topic_end> |
<topic_start> |
2. Add a TextFormField with validation logic |
Although the Form is in place, |
it doesn’t have a way for users to enter text. |
That’s the job of a TextFormField. |
The TextFormField widget renders a material design text field |
and can display validation errors when they occur.Validate the input by providing a validator() function to the |
TextFormField. If the user’s input isn’t valid, |
the validator function returns a String containing |
an error message. |
If there are no errors, the validator must return null.For this example, create a validator that ensures the |
TextFormField isn’t empty. If it is empty, |
return a friendly error message. |
<code_start>TextFormField( |
// The validator receives the text that the user has entered. |
validator: (value) { |
if (value == null || value.isEmpty) { |
return 'Please enter some text'; |
} |
return null; |
}, |
),<code_end> |
<topic_end> |
<topic_start> |
3. Create a button to validate and submit the form |
Now that you have a form with a text field, |
provide a button that the user can tap to submit the information.When the user attempts to submit the form, check if the form is valid. |
If it is, display a success message. |
If it isn’t (the text field has no content) display the error message. |
<code_start>ElevatedButton( |
onPressed: () { |
// Validate returns true if the form is valid, or false otherwise. |
if (_formKey.currentState!.validate()) { |
// If the form is valid, display a snackbar. In the real world, |
// you'd often call a server or save the information in a database. |
ScaffoldMessenger.of(context).showSnackBar( |
const SnackBar(content: Text('Processing Data')), |
); |
} |
}, |
child: const Text('Submit'), |
),<code_end> |
<topic_end> |
<topic_start> |
How does this work? |
To validate the form, use the _formKey created in |
step 1. You can use the _formKey.currentState() |
method to access the FormState, |
which is automatically created by Flutter when building a Form.The FormState class contains the validate() method. |
When the validate() method is called, it runs the validator() |
function for each text field in the form. |
If everything looks good, the validate() method returns true. |
If any text field contains errors, the validate() method |
rebuilds the form to display any error messages and returns false.<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) { |
const appTitle = 'Form Validation Demo'; |
return MaterialApp( |
title: appTitle, |
home: Scaffold( |
appBar: AppBar( |
title: const Text(appTitle), |
), |
body: const MyCustomForm(), |
), |
); |
} |
} |
// Create a 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.