Flutter拨号盘国家代码选择器插件dialcodeselector的使用

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

Flutter拨号盘国家代码选择器插件dialcodeselector的使用

dialcodeselector Package

dialcodeselector 是一个用于选择国家或国家拨号代码的包。

Screens

pubspec.yaml

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

dependencies:
  dialcodeselector: <last version>

import

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

import 'package:dialcodeselector/dialcodeselector.dart';

Simple example

下面是一个简单的示例,展示如何使用 dialcodeselector 包来选择国家和国家代码。

// 定义默认国家
Country? defaultCountry = DialCodeSelector.getCountryByDialCode(dialCode: "233");

// 实现回调函数
void onCountrySelected(Country? country) {
  defaultCountry = country;
  setState(() {});
}

// 定义主题以自定义国家代码选择器的外观
DialCodeSelectorTheme get selectorTheme => 
    DialCodeSelectorTheme(titleColor: Colors.blueGrey);

void showCountryPicker() async {
  await DialCodeSelector.selectCountry(context,
      selectorTheme: selectorTheme,
      initialShortName: defaultCountry?.countryShortName,
      onCountrySelected: onCountrySelected);
}

Features

  • 选择国家拨号代码
  • 选择国家
  • 返回所选国家的名称、标志、短名和拨号代码
  • 通过短名获取国家
  • 通过拨号代码获取国家

完整示例 Demo

以下是完整的示例代码,展示如何在 Flutter 应用程序中集成 dialcodeselector 包。

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 初始化国家列表
  await DialCodeSelector.init();
  runApp(const MyApp());
}

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

  // 这个小部件是应用的根
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Package Example App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomeView(),
    );
  }
}

class HomeView extends StatefulWidget {
  const HomeView({super.key});

  [@override](/user/override)
  State<HomeView> createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  String? initialCountrCode;

  // 设置默认国家(使用国家拨号代码或短名)
  Country? selectedCountry = DialCodeSelector.getCountryByDialCode(dialCode: "233");

  // 定义主题以自定义国家代码选择器的外观
  DialCodeSelectorTheme get selectorTheme => DialCodeSelectorTheme(
    emptySearchResultsView: const Text("Nothing Found"),
    showCountriesOnly: true,
    titleColor: Colors.blueGrey,
  );

  // 实现回调函数
  void onCountrySelected(Country? country) {
    selectedCountry = country;
    setState(() {});
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Home")),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 25),
        child: Center(
          child: TextFormField(
            decoration: InputDecoration(
              hintText: "Enter phone number",
              prefixIcon: MaterialButton(
                padding: EdgeInsets.zero,
                elevation: 0,
                highlightElevation: 0,
                onPressed: () async {
                  await DialCodeSelector.selectCountry(context,
                      selectorTheme: selectorTheme,
                      initialShortName: selectedCountry?.countryShortName,
                      onCountrySelected: onCountrySelected);
                },
                child: Row(mainAxisSize: MainAxisSize.min, children: [
                  const SizedBox(width: 11),
                  Image.asset(selectedCountry?.icon ?? "", height: 21, width: 21),
                  const SizedBox(width: 5),
                  const Icon(Icons.arrow_drop_down),
                  const SizedBox(width: 7),
                  Padding(
                    padding: const EdgeInsets.only(right: 13),
                    child: Text(selectedCountry?.dialCode ?? "", style: const TextStyle(color: Colors.black, fontSize: 16)),
                  )
                ]),
              ),
            ),
            onChanged: (String value) {},
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


当然,以下是如何在Flutter项目中使用dialcodeselector插件的一个示例代码案例。这个插件允许你在应用中实现一个拨号盘国家代码选择器,非常适合国际电话号码输入的场景。

首先,确保你已经在pubspec.yaml文件中添加了dialcodeselector依赖:

dependencies:
  flutter:
    sdk: flutter
  dialcodeselector: ^x.y.z  # 请替换为最新版本号

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

接下来,在你的Flutter应用中,你可以按照以下步骤使用DialCodeSelector组件:

  1. 导入包
import 'package:dialcodeselector/dialcodeselector.dart';
  1. 使用DialCodeSelector组件
import 'package:flutter/material.dart';
import 'package:dialcodeselector/dialcodeselector.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dial Code Selector Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String selectedCountryCode = '';
  String selectedDialCode = '';
  String selectedCountryName = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Dial Code Selector Example'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            DialCodeSelector(
              onChanged: (countryCode, dialCode, countryName) {
                setState(() {
                  selectedCountryCode = countryCode;
                  selectedDialCode = dialCode;
                  selectedCountryName = countryName;
                });
              },
              favorite: ['+1', '91', '+86'], // 可选:设置常用国家代码
              initialSelection: 'IN', // 可选:设置初始选中的国家代码(ISO 3166-1 alpha-2代码)
              showFlag: true, // 可选:是否显示国旗
              showCountryNameCode: true, // 可选:是否显示国家名称和代码
              showDialCodeOnly: false, // 可选:是否只显示拨号代码
              hideSearchBar: false, // 可选:是否隐藏搜索栏
            ),
            SizedBox(height: 20),
            Text('Selected Country Code: $selectedCountryCode'),
            Text('Selected Dial Code: $selectedDialCode'),
            Text('Selected Country Name: $selectedCountryName'),
          ],
        ),
      ),
    );
  }
}

在这个示例中:

  • DialCodeSelector组件用于显示拨号盘国家代码选择器。
  • onChanged回调函数在选中国家代码变化时被调用,并返回选中的国家代码(ISO 3166-1 alpha-2代码)、拨号代码和国家名称。
  • favorite参数用于设置常用国家代码,这些国家代码会在选择器顶部显示。
  • initialSelection参数用于设置初始选中的国家代码。
  • showFlag参数决定是否显示国旗。
  • showCountryNameCode参数决定是否显示国家名称和代码。
  • showDialCodeOnly参数决定是否只显示拨号代码。
  • hideSearchBar参数决定是否隐藏搜索栏。

这个示例展示了如何在Flutter应用中使用dialcodeselector插件来实现一个功能完善的国家代码选择器。希望这个示例对你有帮助!

回到顶部