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

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

国家选择器插件max_country_picker

国家选择器插件max_country_picker提供了三种图标模式(圆形、方形和表情符号),并支持两种视图模式(页面模式和模态底部弹出框)。

截图

App Screenshot

安装

安装

flutter pub add max_country_picker

导入

import 'package:max_country_picker/max_country_picker.dart';

使用

MaxCountryPicker(
  flagMode: FlagMode.square, // 设置标志模式为方形
  viewMode: ViewMode.page, // 设置视图模式为页面模式
  initialCountryCode: 'ID', // 设置初始国家代码为ID
  onCanged: (value) { // 当国家选择改变时回调
      print(value.name); // 打印所选国家的名称
  },
)

参数

字段名 类型 描述
initialCountryCode String 设置初始国家代码
countryCodeStyle TextStyle 自定义主按钮国家代码文本样式
countryNameStyle TextStyle 自定义主按钮国家名称文本样式
showDropDown bool 显示或隐藏按钮下拉图标
showFlagIcon bool 显示或隐藏按钮旗帜图标
showCountryName bool 显示或隐藏按钮国家名称
dropDownColor Color 设置下拉图标颜色
flagIconSize double 设置旗帜图标大小
flagMode FlagMode 更改标志模式为表情符号、圆形或方形
viewMode ViewMode 更改国家视图模式为页面或模态底部弹出框
countryListConfig CountryListConfig 配置国家列表

CountryListConfig 参数

字段名 类型 描述
title String 国家列表视图模式页面和模态的标题
appBarBackButtonColor Color 设置应用栏返回按钮的颜色
appBarCustomBackButtonIcon Widget 自定义页面模式的返回按钮
appBarTitleTextStyle TextStyle 页面模式应用栏标题文本样式
backgroundColor Color 页面模式背景颜色
countryCodeTextStyle TextStyle 国家代码列表文本样式
countryNameTextStyle TextStyle 国家名称列表文本样式
filterExcludeCountry List 过滤想要隐藏的国家
filterOnlyShowingCountry List 过滤只想显示的国家
flagIconSize double 设置国家列表旗帜大小
hideSearchBar bool 显示或隐藏搜索栏
modalBackgoroundColor Color 设置模态背景颜色
modalIndicatorColor Color 设置模态指示器颜色
modalTitleTextStyle TextStyle 模态标题文本样式
searchBackgroundColor Color 搜索栏背景颜色
searchCustomSearchIcon Widget 设置自定义搜索栏图标
searchHintText String 搜索栏提示文本
searchHintTextStyle TextStyle 搜索栏提示文本样式
searchIconColor Color 搜索栏图标颜色
searchRadius double 搜索栏圆角半径
searchTextStyle TextStyle 搜索栏文本样式
separatedColor Color 自定义分隔国家列表颜色
systemOverlayStyle SystemUiOverlayStyle 自定义页面模式系统Overlay样式

支持我们

您的支持可以帮助我们成长 : )


示例代码

import 'dart:developer';

import 'package:example/custom_text_field.dart';
import 'package:flutter/material.dart';
import 'package:max_country_picker/max_country_picker.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var countryCode = 'ID';

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            children: [
              Column(
                children: [
                  CustomTextField(
                    label: '默认',
                    prefixWidget: MaxCountryPicker(
                      onCanged: (country) {
                        log(country.code!);
                      },
                    ),
                  ),
                  CustomTextField(
                    label: '表情模式,过滤国家 [US, ID, SG, MY, GB]',
                    prefixWidget: MaxCountryPicker(
                      flagMode: FlagMode.emoji,
                      viewMode: ViewMode.modal,
                      flagIconSize: 22,
                      initialCountryCode: countryCode,
                      countryListConfig: const CountryListConfig(
                        filterOnlyShowingCountry: ['US', 'ID', 'SG', 'MY', 'GB'],
                      ),
                      onCanged: (country) {
                        log(country.code!);
                      },
                    ),
                  ),
                  CustomTextField(
                    label: '页面模式,自定义样式',
                    prefixWidget: MaxCountryPicker(
                      flagMode: FlagMode.square,
                      viewMode: ViewMode.page,
                      flagIconSize: 22,
                      initialCountryCode: countryCode,
                      countryListConfig: CountryListConfig(
                        backgroundColor: Colors.grey[850],
                        searchBackgroundColor: Colors.grey[800],
                        countryCodeTextStyle: const TextStyle(
                            color: Colors.grey, fontWeight: FontWeight.w500),
                        countryNameTextStyle: const TextStyle(
                            color: Colors.white, fontWeight: FontWeight.w600),
                        appBarTitleTextStyle: const TextStyle(
                          color: Colors.white,
                        ),
                        appBarBackButtonColor: Colors.white,
                        searchIconColor: Colors.grey,
                        searchHintText: '搜索国家',
                        searchHintTextStyle: const TextStyle(
                            fontSize: 14, color: Colors.grey),
                        searchTextStyle: const TextStyle(
                            fontSize: 16, color: Colors.white),
                      ),
                      onCanged: (country) {
                        log(country.code!);
                      },
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


max_country_picker 是一个用于 Flutter 的国家选择器插件,它允许用户从列表中选择国家。这个插件提供了简单的集成方式,并且支持自定义样式和功能。

安装

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

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

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

基本用法

以下是一个简单的示例,展示如何在 Flutter 应用中使用 max_country_picker 来选择国家。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: CountryPickerExample(),
    );
  }
}

class CountryPickerExample extends StatefulWidget {
  @override
  _CountryPickerExampleState createState() => _CountryPickerExampleState();
}

class _CountryPickerExampleState extends State<CountryPickerExample> {
  Country? _selectedCountry;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Country Picker Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            _selectedCountry != null
                ? Text('Selected Country: ${_selectedCountry!.name}')
                : Text('No country selected'),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () async {
                final country = await showCountryPicker(
                  context: context,
                  showPhoneCode: true, // 是否显示电话区号
                  showFlag: true, // 是否显示国旗
                );
                setState(() {
                  _selectedCountry = country;
                });
              },
              child: Text('Select Country'),
            ),
          ],
        ),
      ),
    );
  }
}

参数说明

showCountryPicker 方法有以下可选参数:

  • context: BuildContext,必需。
  • showPhoneCode: 是否显示国家的电话区号,默认为 false
  • showFlag: 是否显示国家的国旗,默认为 false
  • onSelected: 当用户选择国家时的回调函数。
  • exclude: 需要排除的国家代码列表。
  • favorites: 需要置顶的国家代码列表。

自定义样式

你可以通过传递 CountryPickerTheme 来自定义选择器的样式:

final country = await showCountryPicker(
  context: context,
  showPhoneCode: true,
  showFlag: true,
  theme: CountryPickerTheme(
    backgroundColor: Colors.white,
    textStyle: TextStyle(color: Colors.black),
    selectedTextStyle: TextStyle(color: Colors.blue),
    selectedBackgroundColor: Colors.lightBlue[100],
  ),
);
回到顶部