Flutter国家选择器插件pick_country_picker的使用

Flutter国家选择器插件pick_country_picker的使用

国家选择器模态框(CountryPickerModal)

CountryPickerModal 是一个高度可定制的模态框组件,允许用户从列表中选择一个国家。该包设计用于易于使用和广泛的自定义,支持 Material 和 Cupertino 模态样式、自定义列表项、搜索功能等。

特性(Features)

  • 自定义化:可以覆盖默认的国家列表,优先处理国家,并使用自定义的小部件来替换搜索字段、国家列表项和选择图标。
  • 导航栏自定义:对导航栏有完全控制权,可以选择在 Material 和 Cupertino 风格中自定义或隐藏它们。
  • Material 和 Cupertino 支持:可以选择 Material 或 Cupertino 模态样式以匹配应用程序的设计。
  • 搜索功能:包含一个搜索栏,可以快速按名称查找国家。
  • 自定义列表项:可以通过构建函数自定义国家列表项的外观。
  • 选择反馈:可以在所选国家旁边显示自定义图标。

安装(Installation)

  1. 依赖它 在你的包的 pubspec.yaml 文件中添加以下依赖:

    dependencies:
      pick_country_picker: ^3.1.0
    
  2. 安装它 你可以通过命令行安装包:

    使用 pub:

    $ pub get
    

    使用 Flutter:

    $ flutter pub get
    
  3. 导入它 然后在 Dart 代码中使用:

    import 'package:pick_country_picker/pick_country_picker.dart';
    

使用(Usage)

要显示国家选择器模态框,创建一个 CountryPickerModal 组件:

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

void _showCountryPicker(BuildContext context) {
  showModalBottomSheet(
    context: context,
    builder: (BuildContext context) {
      return CountryPickerModal(
        onCountryChanged: (Country country) {
          // 处理国家选择
          print("Selected country: ${country.countryName}");
        },
      );
    },
  );
}

自定义化(Customization)

可以通过设置 CountryPickerModal 的属性来自定义国家选择器模态框:

CountryPickerModal(
  onCountryChanged: (Country country) {},
  selectedCountryCode: 'US',
  title: '选择您的国家',
  // 导航栏自定义
  hideNavigationBar: false,  // 设置为 true 可以完全隐藏导航栏
  appBar: AppBar(  // 自定义 Material AppBar
    title: Text('选择国家'),
    leading: IconButton(
      icon: Icon(Icons.close),
      onPressed: () => Navigator.pop(context),
    ),
  ),
  navigationBar: CupertinoNavigationBar(  // 自定义 Cupertino Navigation Bar
    middle: Text('选择国家'),
    leading: CupertinoButton(
      padding: EdgeInsets.zero,
      child: Text('关闭'),
      onPressed: () => Navigator.pop(context),
    ),
  ),
  // 其他自定义选项
  priorityCountryCodes: ['US', 'CA'],
  countryListItemBuilder: (Country country) {
    return ListTile(
      title: Text(country.countryName),
      leading: Image.asset(country.flagUri),
    );
  },
  selectedIcon: Icon(Icons.check_circle, color: Colors.green),
  overrideCountryCodes: ['US', 'CA', 'GB'],
  excludedCountryCodes: ['AX', 'USA', 'MX'],
  hideSearch: false,
  hideCloseIcon: false,
  backButton: Icon(Icons.arrow_back),
  borderRadius: BorderRadius.only(
    topLeft: Radius.circular(20),
    topRight: Radius.circular(20),
  ),
  borderBuilder: (Country country) {
    return Border.all(
      color: Colors.grey[400]!,
      width: 0.5,
    );
  },
  useCupertinoModal: false,
)

获取国家(Get Country)

通过 ISO 代码获取国家(Get Country by ISO Code)

通过其 ISO 代码程序化地检索国家信息,可以使用 getCountryByIsoCode 函数。此函数会在国家对象列表中搜索并返回与给定 ISO 代码匹配的对象。如果没有找到匹配项,则返回 null

Country? getCountryByIsoCode(String isoCode);

通过国家代码获取国家(Fetch a Country by Its Country Code)

通过国家代码检索国家,可以使用 getCountryByCountryCode 函数。类似于 getCountryByIsoCode,此函数扫描国家对象列表,找到并返回与给定国家代码匹配的对象。如果找不到指定代码的国家,则返回 null

Country? getCountryByCountryCode(String countryCode)

通过国家名称获取国家(Fetch a Country by Its Name)

基于国家名称查找国家,可以使用 getCountryByName 函数。此函数在国家对象列表中搜索,返回与提供的名称匹配的国家。如果搜索结果为空,则函数返回 null

Country? getCountryByName(String countryName)

使用示例:

final _pickCountryLookupService = PickCountryLookupService();

Country? c = _pickCountryLookupService.getCountryByIsoCode("uk");

更多关于Flutter国家选择器插件pick_country_picker的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


pick_country_picker 是一个用于 Flutter 的国家选择器插件,可以帮助用户在应用中选择国家信息。以下是如何使用 pick_country_picker 的基本步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  pick_country_picker: ^latest_version  # 请替换为最新版本

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

2. 导入包

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

import 'package:pick_country_picker/pick_country_picker.dart';

3. 使用 pick_country_picker

你可以通过调用 showCountryPicker 方法来显示国家选择器。以下是一个简单的示例:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Country Picker Example')),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              _showCountryPicker(context);
            },
            child: Text('Select Country'),
          ),
        ),
      ),
    );
  }

  void _showCountryPicker(BuildContext context) async {
    final country = await showCountryPicker(
      context: context,
      showPhoneCode: true, // 是否显示电话区号
    );

    if (country != null) {
      print('Selected Country: ${country.name}');
      print('Phone Code: ${country.phoneCode}');
    }
  }
}

4. 参数说明

showCountryPicker 方法接受多个参数,以下是一些常见的参数:

  • context: 必须的 BuildContext,用于显示对话框。
  • showPhoneCode: 是否显示国家电话区号,默认为 false
  • showNationality: 是否显示国家国籍名称,默认为 false
  • showCurrency: 是否显示国家货币,默认为 false
  • showFlag: 是否显示国旗,默认为 true
  • showName: 是否显示国家名称,默认为 true

5. 处理选择结果

当用户选择了一个国家后,showCountryPicker 会返回一个 Country 对象,你可以通过该对象访问国家名称、电话区号等信息。

6. 自定义样式

你可以通过 countryTheme 参数来自定义国家选择器的样式。例如:

showCountryPicker(
  context: context,
  countryTheme: CountryTheme(
    flagSize: 25,
    textStyle: TextStyle(fontSize: 16),
    borderRadius: BorderRadius.circular(8),
    inputDecoration: InputDecoration(
      labelText: 'Search',
      hintText: 'Start typing to search',
    ),
  ),
);
回到顶部