caseyryan/flutter_multi_formatter

It's not possible to print '+' sign in PhoneNumberFormatter

Closed this issue · 8 comments

Is it possible to allow user to input '+' sign? It's a bit strange, because '+' is eventually printed anyway.

I don't think it's ever necessary

'+' printed only if country detected.
You cant try wrap PhoneInputFormatter in you custom input formater, that handle when '+' is type.

@caseyryan User starts typing his international number, and sees that '+' is not even printed.
It affects UX badly. So why is it not necessary?

P.S. Why '-' is allowed then?

@karabanovbs I already tried. And it's not really possible, 'cause formatter erases +, if it doesn't find country, which has this digit.
For example, when I type '+7' , it persists. But when I type '+4', '+' is erased (result is just '4').

@AlexandrFarkas
It is possible. Something like this:

class AllowPlusCodeFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
      var origin = PhoneInputFormatter.formatEditUpdate(oldValue, newValue);
    if (!origin.text.startsWith('+') && newValue.text.startsWith('+') {
      return newValue;
    } else {
      return origin;
    }
  }
}

You also can deny remove plus.

class RequiredPlusCodeFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    if (!newValue.text.startsWith('+')) {
      return TextEditingValue(
        text: '+${newValue.text}',
        selection: TextSelection.collapsed(
          offset: '+${newValue.text}'.length,
        ),
      );
    } else {
      return newValue;
    }
  }
}

@karabanovbs Thanks a lot for help. Closing

@karabanovbs I already tried. And it's not really possible, 'cause formatter erases +, if it doesn't find country, which has this digit.
For example, when I type '+7' , it persists. But when I type '+4', '+' is erased (result is just '4').

What country code starts with +4? I know only one country whose phone code starts with +4 but its +44, in UK. If you enter 44 plus sign is prepended just fine. If there is such a country and it's not on the list, I will add it. But the way you propose to solve the problem is not correct. It ruins the whole idea of an automated formatter. It formats a phone according to a phone mask accepted in a country. Each country has one or more phone masks which are accepted. Just "formatting" it somehow will not work in this case. That's why + is prepended automatically and there is absolutely no way a user ever needs to type it by hand