I am using a TextFormField
as a widget and TextEditingController
inside TextFormField for a phone number, and I am trying to validate a number while typing with the flutter_libphonenumber
library, which provides an async function for validation.
I am trying to change the colour of the text depending upon validation status while entering it.
Current state:
- Implemented
TextFormField
and providing itTextEditingController
as a controller with valid initial text. - Implemented
onFieldSubmitted
and it works fine on tap of Done button of the keyboard. - Added
listener
to this controller for validation and set _isValid bool there. - I have used this bool to set colour of text inside the controller while building it.
- So the current issue is, when I am entering text I get proper validated status inside the listener, but the colour of text is not changing accordingly.
I understand this is happening due to not calling setState
after validation, so it will rebuild the widget and change the colour according to _isValid.
Error: When I try to setState
inside the listener
, it just doesn't allow editing.
Note: I have tried the same approach using onChanged
method and it also does same, cannot enter or delete anything inside textField if there is pre-populated text (initialText
).
Can anyone help me to solve this issue, or guide the correct path?
Thank you!