text stringlengths 1 372 |
|---|
// 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>. |
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.