I am new to Flutter (and Dart) and when trying to build a form to edit an object I searched online for examples and tutorials, and I saw both of these used.
What is the difference between the two? Which one should I use?
TextFormField widget is used to take input from the user in flutter. This is a simple and easy user input widget in flutter. We can perform any operation on that user input data using TextFormField. You can use that user input, can send and show that input. You can store it into a TextEditingController type object.
The major difference between a textarea and a text field ( ), is that a text field only has one line, whereas a textarea usually has multiple lines.
onSaved property Null safetyAn optional method to call with the final value when the form is saved via FormState. save.
If you making a
Form
where you require save, reset, or validate operations- useTextFormField
. Else For Simple user input captureTextField
is sufficient.
TextFormField
, which integrates with the Form
widget.
This is a convenience widget that wraps a TextField widget in a FormField.
A Form
ancestor is not required. The Form simply makes it easier to save, reset, or validate multiple fields at once.
To use without a Form, pass a GlobalKey to the constructor and use GlobalKey.currentState to save or reset the form field.
sample:
TextFormField( autovalidateMode: AutovalidateMode.always decoration: const InputDecoration( icon: Icon(Icons.person), hintText: 'What do people call you?', labelText: 'Name *', ), onSaved: (String value) { // This optional block of code can be used to run // code when the user saves the form. }, validator: (String value) { return value.contains('@') ? 'Do not use the @ char.' : null; }, )
TextField
, which is the underlying text field without the Form
integration.
The text field calls the onChanged
callback whenever the user changes the text in the field. If the user indicates that they are done typing in the field (e.g., by pressing a button on the soft keyboard), the text field calls the onSubmitted
callback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With