I need help with this feature. When I enter a text in a textField, I need you to validate that text when I enter another textField.
This is my code
class _ProfileState extends State<Profile> {
bool _hasInputError = false;
var idNumber = '';
var nombreUser = "";
FocusNode focusNode;
void initState() {
super.initState();
focusNode = new FocusNode();
focusNode.addListener(() {
if (!focusNode.hasFocus) {
setState(() {
_hasInputError = idNumber.length < 3;
});
}
});
}
@override
Widget build(BuildContext context) {
final nombreUserField = TextField(
focusNode: _focusNode,
onChanged: (String text) {
nombreUser = text;
},
);
final idNumberElement = TextField(
focusNode: _focusNode,
decoration: InputDecoration(
errorText: _hasInputError ? "Id is too short" : null,
counterText: "",
),
onChanged: (String tex) {
idNumber = tex;
},
);
return WillPopScope(
child: Scaffold(
body: Listener(
onPointerUp: (e) {
FocusScope.of(context).requestFocus(FocusNode());
},
child: SingleChildScrollView(
child: Container(child: Column(
children: <Widget>[
SizedBox(
height: 10,
),
idNumberElement,
SizedBox(
height: 20,
),
nombreUserField,
],
),
),
),
),
));
}
}
I tried to make the validation appear in the onEditingComplete but it doesn't work. I Try this answer but it's doesn't work.