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

发布于 1周前 作者 htzhanglong 来自 Flutter

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

标题

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

内容

  • Pub链接: https://pub.dev/packages/country_picker_flutter

  • 插件描述: 该插件用于显示国家代码选择器。它支持70种语言的国际化。

  • 示例图片:

  • 使用说明:

    • 只需将组件放入应用程序中,并设置onChanged回调。
import 'package:country_code_picker_mp/country_code_picker.dart';
import 'package:flutter/material.dart';

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: CountryCodePicker(
        onChanged: print,
        // 初始选择和喜爱列表可以是代码('IT')或拨号码('+39')
        initialSelection: 'IT',
        favorite: ['+39', 'FR'],
        countryFilter: ['IT', 'FR'],
        showFlagDialog: false,
        comparator: (a, b ) => b.name!.compareTo(a.name!),
        // 获取初始选择相关的国家信息
        onInit: ( code ) => print(
          "on init ${code!.name} ${code.dialCode} ${code.name}"
        ),
      ),
    ),
  );
}
  • 示例:
void _onCountryChange(CountryCode countryCode) {
  // TODO : 操作所选国家代码
  log("New Country selected: " + countryCode.toString());
}

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('CountryPicker Example'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            CountryCodePicker(
              onChanged: print,
              // 初始选择和 喜爱列表可以是代码('IT')或拨号码('+39')
              initialSelection: 'IT',
              favorite: ['+39', 'FR'],
              countryFilter: ['IT', 'FR'],
              showFlagDialog: false,
              comparator: ( a, b ) => b.name!.compareTo(a.name!),
              // 获取初始选择相关的国家信息
              onInit: ( code ) => print(
                "on init ${code!.name} ${code.dialCode} ${code.name}"
              ),
            ),
            CountryCodePicker(
              onChanged: print,
              // 初始选择和 喜爱列表可以是代码('IT')或拨号码('+39')
              initialSelection: 'IT',
              favorite: ['+39', 'FR'],
              countryFilter: ['IT', 'FR'],
              // 国旗可以使用 `Decoration` 的的 `borderRadius` 和 `shape` 字段进行样式化
              flagDecoration: BoxDecoration(
                borderRadius: BorderRadius.circular(7),
              ),
            ),
            SizedBox(
              width: 400,
              height: 60,
              child: CountryCodePicker(
                onChanged: print,
                hideMainText: true,
                showFlagMain: true,
                showFlag: false,
                initialSelection: 'TF',
                hideSearch: true,
                showCountryOnly: true,
                showOnlyCountryWhenClosed: true,
                alignLeft: true,
              ),
            ),
            SizedBox(
              width: 400,
              height: 60,
              child: CountryCodePicker(
                onChanged: ( e ) => print(e.toLongString()),
                initialSelection: 'TF',
                showCountryOnly: true,
                showOnlyCountryWhenClosed: true,
                favorite: ['+39', 'FR'],
              ),
            ),
            SizedBox(
              width: 100,
              height: 60,
              child: CountryCodePicker(
                enabled: false,
                onChanged: ( c ) => c.name,
                initialSelection: 'TF',
                showCountryOnly: true,
                showOnlyCountryWhenClosed: true,
                favorite: ['+39', 'FR'],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
  • i18n:
    • 只需在应用的 delegates 列表中添加 CountryLocalizations.delegate
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: [
    CountryLocalizations.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
);

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

1 回复

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


当然,以下是如何在Flutter项目中使用country_code_picker_mp插件的示例代码。这个插件允许用户从下拉列表中选择国际电话号码的国家代码,并显示相应的国旗和拨号代码。

步骤1:添加依赖

首先,在你的pubspec.yaml文件中添加country_code_picker_mp依赖:

dependencies:
  flutter:
    sdk: flutter
  country_code_picker_mp: ^0.0.5  # 请注意版本号,使用最新版本

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

步骤2:导入插件

在你的Flutter项目的Dart文件中导入country_code_picker_mp

import 'package:country_code_picker_mp/country_code_picker_mp.dart';

步骤3:使用插件

下面是一个简单的示例,展示了如何在Flutter中使用CountryCodePicker小部件:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('CountryCodePicker Demo'),
        ),
        body: Center(
          child: CountryCodePickerDemo(),
        ),
      ),
    );
  }
}

class CountryCodePickerDemo extends StatefulWidget {
  @override
  _CountryCodePickerDemoState createState() => _CountryCodePickerDemoState();
}

class _CountryCodePickerDemoState extends State<CountryCodePickerDemo> {
  String _phoneNumber = "";
  String _dialCode = "";
  String _countryCode = "";

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        CountryCodePickerMP(
          initialSelection: "US",  // 初始选择
          favorite: ["+1", "CN"],  // 收藏的国家代码
          onChanged: (dialCode, countryCode) {
            setState(() {
              _dialCode = dialCode!;
              _countryCode = countryCode!;
            });
          },
          onPhoneNumberChanged: (phoneNumber) {
            setState(() {
              _phoneNumber = phoneNumber!;
            });
          },
          textStyle: TextStyle(fontSize: 18),
          decoration: InputDecoration(
            border: OutlineInputBorder(),
            labelText: 'Phone Number',
          ),
        ),
        SizedBox(height: 20),
        Text("Dial Code: $_dialCode"),
        Text("Country Code: $_countryCode"),
        Text("Phone Number: $_phoneNumber"),
      ],
    );
  }
}

解释

  1. 依赖导入:在pubspec.yaml文件中添加country_code_picker_mp依赖。
  2. 导入插件:在Dart文件中导入country_code_picker_mp
  3. 使用插件
    • 创建一个CountryCodePickerMP小部件。
    • 设置initialSelection为初始选择的国家代码。
    • 使用favorite参数来定义收藏的国家代码列表。
    • 使用onChanged回调来更新拨号代码和国家代码。
    • 使用onPhoneNumberChanged回调来更新电话号码。
    • 使用textStyledecoration来自定义小部件的样式。

希望这个示例代码能帮助你在Flutter项目中使用country_code_picker_mp插件。如果你有任何其他问题,请随时告诉我!

回到顶部