首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于显示国家代码选择器(默认值)的颤振包

用于显示国家代码选择器(默认值)的颤振包
EN

Stack Overflow用户
提问于 2022-02-23 18:14:15
回答 1查看 1.9K关注 0票数 0

如何根据电话中选定的国家设置国家代码选择器(默认值)

代码语言:javascript
运行
复制
    Widget build(BuildContext context) => new Scaffold(
     body: Center(
       child: CountryCodePicker(
         onChanged: print,
         // Initial selection and favorite can be one of code ('IT') OR dial_code('+39')
         initialSelection: 'IT',
         favorite: ['+39','FR'],
         // optional. Shows only country name and flag
         showCountryOnly: false,
         // optional. Shows only country name and flag when popup is closed.
         showOnlyCountryWhenClosed: false,
         // optional. aligns the flag and the Text left
         alignLeft: false,
       ),
     ),
 );
EN

回答 1

Stack Overflow用户

发布于 2022-02-23 22:50:16

看起来您使用的是包country_code_picker。

您可以从window.locale获得一些关于用户默认语言首选项的有用信息,在这里,您可以使用countryCodelanguagueCode等属性来设置默认语言。尽管如此,这并不确定这是用户首选的语言,但仍然是一个指标。

此包似乎在包中公开的名为List<Map<String,String>>codes中列出了受支持的国家。因此,为了安全起见,应该根据这个列表检查来自window.locale.countryCode的数据。

示例:

代码语言:javascript
运行
复制
import 'dart:ui';

import 'package:country_code_picker/country_code_picker.dart';
import 'package:country_code_picker/country_codes.dart';
import 'package:flutter/material.dart';
import 'package:collection/collection.dart';

class CountryPickerWithLocale extends StatefulWidget {
  const CountryPickerWithLocale({Key? key, required this.onCountryChanged})
      : super(key: key);

  final Function(CountryCode) onCountryChanged;

  @override
  State<CountryPickerWithLocale> createState() =>
      _CountryPickerWithLocaleState();
}

class _CountryPickerWithLocaleState extends State<CountryPickerWithLocale> {
  late String userCountryCode;
  String fallbackCountryCode = "UK";

  @override
  void initState() {
    super.initState();

    // Get the language set as default on the users phone
    String? systemCountryCode = window.locale.countryCode;

    // The package you are using has supported countries defined inside a
    // "codes" map.
    Map<String,String>? supportedLanguage = codes.firstWhereOrNull((element) {
       return element["code"] == systemCountryCode;
    });
    
    // Use a fallback if the language is unsupported in the package, or if 
    // there are some issues with retrieving the country code from the locale.
    userCountryCode = supportedLanguage?["code"] ?? fallbackCountryCode;
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: CountryCodePicker(
        onChanged: widget.onCountryChanged,
        initialSelection: userCountryCode,
        showCountryOnly: false,
        showOnlyCountryWhenClosed: false,
        alignLeft: false,
      ),
    );
  }
}

class CountryPickerScreen extends StatelessWidget {
  const CountryPickerScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CountryPickerWithLocale(
          onCountryChanged: (code) {
            print(code.code);
          },
        ),
      ),
    );
  }
}

// Some code to run the above example.
class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: CountryPickerScreen());
  }
}

void main() => runApp(const App());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71242134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档