0

I was trying to get a country's 2 letter code using the calling code.

For example it would be getting the Country Code 'MY' from its calling code '+60'

I want to use it to set the initialcountryvalue for intlphonefield.

Is there a way to do that in flutter?

凯文Beno
  • 55
  • 6
  • libphonenumber: https://github.com/google/libphonenumber https://pub.dev/packages/libphonenumber – Dai Sep 06 '22 at 03:04
  • 1
    Do you have any code or examples of what exactly you're working with? It would be simple to just keep a map of calling codes to country codes and then look up the calling code - unless there's something I'm missing. Hard to determine without any real context. – Layne Bernardo Sep 06 '22 at 03:04
  • I was using intlphonefield but I need to read some data that contains a phone number eg: +60xxxxxxxxxx so I want to read the +60 part and insert it into the phonefield. But the phonefield only accepts 2 letter country code such as 'MY' for Malaysia – 凯文Beno Sep 06 '22 at 06:26

1 Answers1

0

There is a package country_code_picker. Also there is country_picker 2.0.16 from where you can pick code too.

Usage:

@override
 Widget build(BuildContext context) => new Scaffold(
     body: new Center(
       child: new CountryCodePicker(
         onChanged: print,
         // Initial selection and favorite can be one of code ('IT') OR dial_code('+39')
         initialSelection: 'IT',
         favorite: ['+39','FR'],
         // optional. Shows only country name and flag
         showCountryOnly: false,
         // optional. Shows only country name and flag when popup is closed.
         showOnlyCountryWhenClosed: false,
         // optional. aligns the flag and the Text left
         alignLeft: false,
       ),
     ),
 );

To get the country code:

void _onCountryChange(CountryCode countryCode) {
    //Todo : manipulate the selected country code here
    print("New Country selected: " + countryCode.toString());
  }
gretal
  • 1,092
  • 6
  • 14