Flutter国际电话号码选择插件smart_country_code_picker的使用

smart_country_code_picker 是一个用于在 Flutter 应用程序中显示国家代码选择器的插件。它支持多达 70 种语言的国际化(i18n)。

插件smart_country_code_picker使用方法

只需将组件添加到您的应用程序中,并设置 onChanged 回调即可。

示例代码

import 'package:flutter/material.dart';
import 'package:smart_country_code_picker/smart_country_code_picker.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('智能国家代码选择器示例'),
        ),
        body: Center(
          child: SmartCountryCodePicker(
            onChanged: (value) {
              print("新选择的国家代码: $value");
            },
            // 初始选择的国家代码或拨号代码
            initialSelection: 'US',
            favorite: ['+1', 'CA'], // 喜爱的国家列表
            // 只显示国家名称和旗帜
            showCountryOnly: false,
            // 关闭时只显示国家名称和旗帜
            showOnlyCountryWhenClosed: false,
            // 对齐标志和文本到左侧
            alignLeft: false,
          ),
        ),
      ),
    );
  }
}

示例解释

  • onChanged: 当用户选择不同的国家代码时触发的回调函数。
  • initialSelection: 设置初始选中的国家代码或拨号代码。
  • favorite: 设置喜爱的国家列表,这些国家会优先显示。
  • showCountryOnly: 如果为 true,则仅显示国家名称和旗帜。
  • showOnlyCountryWhenClosed: 如果为 true,则在关闭弹窗时仅显示国家名称和旗帜。
  • alignLeft: 如果为 true,则对齐标志和文本到左侧。

国际化(i18n)

要启用插件的多语言支持,只需在应用的 localizationsDelegates 中添加 CountryPickerLocalizations.delegate

示例代码

import 'package:flutter/material.dart';
import 'package:smart_country_code_picker/smart_country_code_picker.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      supportedLocales: [
        Locale("af"),
        Locale("am"),
        Locale("ar"),
        Locale("az"),
        Locale("be"),
        Locale("bg"),
        Locale("bn"),
        Locale("bs"),
        Locale("ca"),
        Locale("cs"),
        Locale("da"),
        Locale("de"),
        Locale("el"),
        Locale("en"),
        Locale("es"),
        Locale("et"),
        Locale("fa"),
        Locale("fi"),
        Locale("fr"),
        Locale("gl"),
        Locale("ha"),
        Locale("he"),
        Locale("hi"),
        Locale("hr"),
        Locale("hu"),
        Locale("hy"),
        Locale("id"),
        Locale("is"),
        Locale("it"),
        Locale("ja"),
        Locale("ka"),
        Locale("kk"),
        Locale("km"),
        Locale("ko"),
        Locale("ku"),
        Locale("ky"),
        Locale("lt"),
        Locale("lv"),
        Locale("mk"),
        Locale("ml"),
        Locale("mn"),
        Locale("ms"),
        Locale("nb"),
        Locale("nl"),
        Locale("nn"),
        Locale("no"),
        Locale("pl"),
        Locale("ps"),
        Locale("pt"),
        Locale("ro"),
        Locale("ru"),
        Locale("sd"),
        Locale("sk"),
        Locale("sl"),
        Locale("so"),
        Locale("sq"),
        Locale("sr"),
        Locale("sv"),
        Locale("ta"),
        Locale("tg"),
        Locale("th"),
        Locale("tk"),
        Locale("tr"),
        Locale("tt"),
        Locale("uk"),
        Locale("ug"),
        Locale("ur"),
        Locale("uz"),
        Locale("vi"),
        Locale("zh"),
      ],
      localizationsDelegates: [
        CountryPickerLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('智能国家代码选择器多语言示例'),
      ),
      body: Center(
        child: SmartCountryCodePicker(
          onChanged: (value) {
            print("新选择的国家代码: $value");
          },
          initialSelection: 'US',
          favorite: ['+1', 'CA'],
          showCountryOnly: false,
          showOnlyCountryWhenClosed: false,
          alignLeft: false,
        ),
      ),
    );
  }
}
1 回复

更多关于Flutter国际电话号码选择插件smart_country_code_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


smart_country_code_picker 是一个用于 Flutter 的国际电话号码选择插件,它允许用户从国家列表中选择国家代码,并返回相应的国家代码和国旗。这个插件非常适合在需要用户输入国际电话号码的应用中使用。

安装

首先,你需要在 pubspec.yaml 文件中添加 smart_country_code_picker 依赖:

dependencies:
  flutter:
    sdk: flutter
  smart_country_code_picker: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来安装依赖。

基本使用

  1. 导入包

    在你的 Dart 文件中导入 smart_country_code_picker 包:

    import 'package:smart_country_code_picker/smart_country_code_picker.dart';
  2. 显示国家代码选择器

    你可以使用 showCountryPicker 方法来显示国家代码选择器。这个方法会返回一个 CountryCode 对象,其中包含用户选择的国家代码、国家名称和国旗。

    void _showCountryPicker() async {
      final CountryCode? countryCode = await showCountryPicker(
        context: context,
      );
    
      if (countryCode != null) {
        print('Selected Country Code: ${countryCode.code}');
        print('Selected Country Name: ${countryCode.name}');
        print('Selected Country Flag: ${countryCode.flag}');
      }
    }
  3. 在按钮中触发选择器

    你可以在按钮的 onPressed 事件中调用 _showCountryPicker 方法来触发国家代码选择器。

    ElevatedButton(
      onPressed: _showCountryPicker,
      child: Text('Select Country Code'),
    );

自定义选项

showCountryPicker 方法还提供了一些可选参数,允许你自定义选择器的行为:

  • searchHint: 搜索框的提示文本。
  • showCountryCode: 是否显示国家代码。
  • showCountryName: 是否显示国家名称。
  • showFlag: 是否显示国旗。
  • favoriteCountries: 设置常用国家列表。

例如:

void _showCountryPicker() async {
  final CountryCode? countryCode = await showCountryPicker(
    context: context,
    searchHint: 'Search for a country',
    showCountryCode: true,
    showCountryName: true,
    showFlag: true,
    favoriteCountries: ['US', 'CN', 'IN'],
  );

  if (countryCode != null) {
    print('Selected Country Code: ${countryCode.code}');
    print('Selected Country Name: ${countryCode.name}');
    print('Selected Country Flag: ${countryCode.flag}');
  }
}

处理选择结果

当用户选择了一个国家代码后,你可以将 CountryCode 对象中的信息用于你的应用逻辑。例如,你可以将国家代码显示在文本框中,或者将其与用户输入的电话号码结合使用。

String? selectedCountryCode;

void _showCountryPicker() async {
  final CountryCode? countryCode = await showCountryPicker(
    context: context,
  );

  if (countryCode != null) {
    setState(() {
      selectedCountryCode = countryCode.code;
    });
  }
}

[@override](/user/override)
Widget build(BuildContext context) {
  return Column(
    children: [
      ElevatedButton(
        onPressed: _showCountryPicker,
        child: Text('Select Country Code'),
      ),
      if (selectedCountryCode != null)
        Text('Selected Country Code: $selectedCountryCode'),
    ],
  );
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!