bhrott / flutter-masked-text

A masked text for Flutter.

Home Page:https://pub.dartlang.org/packages/flutter_masked_text

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUGFIX] Editing a character in the middle of the text (possible solution).

FabioBachi opened this issue · comments

I was going through a problem when editing a value in a masked input. For example, if the user typed his phone "(54) 99999-9999" and wanted to change it to "(54) 94999-9999" the input would put the number 4 in the end of the text, like "(54) 99999-9994".

So, following the initial suggestion made by Marc in the #16 , i change the text method to this:

@override
set text(String newText) {
  print(newText);
  if (super.text != newText) {
    int position = this.selection.baseOffset >= super.text.length
        ? (newText ?? '').length
        : this.selection.baseOffset;
    value = value.copyWith(
        text: newText,
        selection: TextSelection.collapsed(offset: position),
        composing: TextRange.empty);
  }
}

Basically, I only put the cursor at the end of the text, if it was already at the end.

Hope it helps (:

thanks but this is not working correctly either. Imagine value 15,000,000.
changing it into 12,000,000 or 10,000,000 works perfectly fine. but when you try to change it into 15,500,000 you endup with value 15,000,005 :D anyway thank you.