如何使用 flutter 获取电话国家代码?
(+972, +92)
我尝试过与localOf. 但我无法获取国家代码。
Locale myLocale = Localizations.localeOf(context);
Run Code Online (Sandbox Code Playgroud)
输出是US, or en-US
Localizations没有电话代码,您可以使用库或使用国家/地区代码和电话代码定义地图,然后按国家/地区访问它。
static const countryPhoneCodes = {"US": "+1", "AR": "+54"};
print('${countryPhoneCodes['US']}'); // output: +1
Run Code Online (Sandbox Code Playgroud)
那么在你的情况下你将能够做这样的事情:
print('${countryPhoneCodes[myLocale.countryCode]}');
Run Code Online (Sandbox Code Playgroud)
从这个库country_pickers中,您可以在此处获取代码:country_codes.dart或从country_code_picker此处获取代码country_codes.dart,或者在其他地方搜索代码。