intl_phone_field icon indicating copy to clipboard operation
intl_phone_field copied to clipboard

initialCountryCode do not work with country dial code

Open sebNiji opened this issue 10 months ago • 2 comments

When we put dial code in initialCountryCode, the result is not correct :

For example :

IntlPhoneField( initialCountryCode: '+33', )

=> The result will show a initial country code to +93 (first country in the countries list).

If we put the ISO code, it work as expected.

sebNiji avatar Apr 15 '24 15:04 sebNiji

I think that the solution should be to change the following code in intl_phone_field.dart :

Line 319 :

else {
      _selectedCountry = _countryList.firstWhere((item) => item.code == (widget.initialCountryCode ?? 'US'),
          orElse: () => _countryList.first);

      // remove country code from the initial number value
      .....

By this one :

else {
      if(widget.initialCountryCode != null && widget.initialCountryCode!.startsWith('+')) {
        _selectedCountry = _countryList.firstWhere((item) => item.dialCode == widget.initialCountryCode!.substring(1),
            orElse: () => _countryList.first);
      } else {
        _selectedCountry = _countryList.firstWhere((item) => item.code == (widget.initialCountryCode ?? 'US'),
            orElse: () => _countryList.first);
      }

      // remove country code from the initial number value
      ....

sebNiji avatar Apr 15 '24 17:04 sebNiji

getCountryCodeFromDialCode(String? dialCode){ if(dialCode==null) return 'UG'; var _selectedCountry = countries.firstWhere((item) => item.dialCode == dialCode.replaceAll("+", '')); return _selectedCountry.code; }

'countries' list is provided in the package itself.

razaen801 avatar Aug 15 '24 07:08 razaen801