Ваш код не работает, потому что вы не меняете состояние родительского виджета
внутри его onChanged:
собственности. Вы создаете новый виджет в заданном состоянии
child: TextField( //parent
decoration: InputDecoration(
hintText: "Enter Email Id",
border: OutlineInputBorder()),
onChanged: (String value) {
emailId = value;
setState(() {
isEmail(value)
? print("true")
: TextField( //this is not the same widget.
decoration: InputDecoration(
errorText: "Enter valid email"),
);
});
},
Вы можете решить эту проблему, объявив String invalidEmailError
и установив для errorText
свойство TextField
. Позже обновите эту строку, чтобы получить желаемый результат.
TextField(
decoration: InputDecoration(
hintText: "Enter Email Id",
errorText: invalidEmailError,
border: OutlineInputBorder()),
onChanged: (String value) {
emailId = value;
setState(() {
isEmail(value)
? invalidEmailError = null
: invalidEmailError = "Enter valid email";
});
},
)