Home > Software engineering >  Is there a way to get the country code according to their calling number
Is there a way to get the country code according to their calling number

Time:09-07

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?

CodePudding user response:

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());
  }
  • Related