Hello everyone I am facing issue in the obscuring text in Flutter
Requirememt:
Obscure the first 5 characters of the Text while entering in TextFormField.
Example : XXXXX4307R
But I have to obscure the text in the TextFormField and also I have to preserve the original plain text on other side.
The problem here I face is whenever I enters the incorrect character in the TextFormField and taps backspace and enters new character again the onChange() method triggers and getting the latest replaced value and returning the value in XXXXX4307R i.e it is overriding the original text stored in the variable.
So we have to preserve the original plain text and also the obsucred text on front side of the application in TextFormField.
Please help with any new idea we can solve this in Flutter Mobile Development.
I am using replaceRange() function for replacing the text with ‘X’ character but once the onChange is triggered it is overriding the exisiting original text stored in the variable.
2
Answers
You can extend
TextEditingController
and override itsbuildTextSpan
method to achieve that behavior:Please note that this is only a minimal implementation for sample purpose. It’s working, but you might need to be aware of other aspects from the original
buildTextSpan
method that you might need to implement as well in the overriding method.to handle this issue efficiently, you need a strategy that maintains two separate variables: one for the obscured text shown in the TextFormField and another for the original text. Here’s a simplified approach that can help you achieve this:
Here’s an example code for reference: