I just want to make my TextField as required field, in which I am using Email and password to login for the user. Please let me know how can I make it required and if user don't fill it, how can I give him warning.
TextField (
onChanged: (value) {
email=value;
},
style: const TextStyle(color: Colors.black),
decoration: InputDecoration(
fillColor: Colors.grey.shade100,
filled: true,
hintText: "Email",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
)
),
),
const SizedBox(
height: 30,
),
TextField(
onChanged: (value) {
password=value;
},
style: const TextStyle(),
obscureText: true,
decoration: InputDecoration(
fillColor: Colors.grey.shade100,
filled: true,
hintText: "Password",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
)
),
),
CodePudding user response:
The esiest way to set a validation logic for the TextField
in Flutter is to use TextFormField
instead.
It provides you with a callback called validator
which is called whenever you call .validate()
method in a Form Key.
To learn more about using Form
widget in Flutter along with TextFormFiled
and validation, check out this video.
Example for the condition in the validator to make the field required:
validator: (String? value) {
if (value == null)
{
return 'This field is required';
}
return null;
},
NOTE:
If the validator
callback returned a message, this means the message would be displayed in the errorText
for the TextFormField
and the .validate()
method would return false
.
If the validator
callback returned null, this means that no errors and the .validate()
method would return true
.
CodePudding user response:
- if user click on submit button then you can check for is email or password field is empty or not empty.