Flutter how can i get the value that from country code that i've been used from my app

Viewed 562

i got the code from pub.dev but its a bit complicated for me to understand when i try to get the value that on the ccp. this is the code what they give me

void _onCountryChange(CountryCode countryCode) {
    //TODO : manipulate the selected country code here
    print("New Country selected: " + countryCode.toString());
  }

how can i get the value and pass it to other place

i already implement the ccp that look like this

CountryCodePicker(
      initialSelection: "ID",
      favorite: ['+62', "ID"],
),

it works fine when i did the onChange: print, in the CountryCodePicker. they give the exact thing that i want

1 Answers

you can have a string called picked country code, and on selectig a country code you call setstate and update its value.

String? pickedCountryCode;

void _onCountryChange(CountryCode countryCode) {
    //TODO : manipulate the selected country code here
    print("New Country selected: " + countryCode.toString());
setState(() {
        pickedCountryCode =countryCode.toString();
      });

  }



and then your country code picker

CountryCodePicker(
                onChanged: _onCountryChange,
                // Initial selection and favorite can be one of code ('IT') OR dial_code('+39')
                initialSelection: 'IT',
                favorite: ['+39', 'FR'],
                countryFilter: ['IT', 'FR'],
                // flag can be styled with BoxDecoration's `borderRadius` and `shape` fields
                flagDecoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(7),
                ),
              ),

Related