You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中DropdownMenu显示CountryFlag仅出现问号的问题求助

解决DropdownMenu中CountryFlag显示问号的问题

首先排查最可能的原因:方法调用错误

你现在用的是CountryFlag.fromLanguageCode('in'),这个方法接收的是ISO 639语言代码(比如英语'en'、印地语'hi'),而非国家代码。如果要显示对应国家的国旗,应该用CountryFlag.fromCountryCode(),并且传入符合ISO 3166-1 alpha-2标准的两位大写国家代码,比如印度的国旗应该写:

CountryFlag.fromCountryCode(
  'IN', // 注意是大写的国家代码
  width: 20,
  height: 20,
)

如果换了方法还是显示问号,按以下步骤排查:

  • 先在普通容器里单独测试CountryFlag组件,确认它能正常显示,排除DropdownMenu的影响:
    Container(
      child: CountryFlag.fromCountryCode('IN', width:20, height:20),
    )
    
  • 检查pubspec.yaml是否正确添加了country_flags依赖,并且执行过flutter pub get
  • 尝试给CountryFlag指定前景色,避免和DropdownMenu的背景色冲突:
    CountryFlag.fromCountryCode(
      'IN',
      width: 20,
      height: 20,
      foregroundColor: Colors.black,
    )
    

替换后的完整DropdownMenu代码示例:

dropdownMenuEntries: list.map<DropdownMenuEntry<String>>((String value) {
  return DropdownMenuEntry<String>(
      value: value,
      label: value,
      labelWidget: CountryFlag.fromCountryCode(
        'IN',
        width: 20,
        height: 20,
      ));
}).toList(),

内容的提问来源于stack exchange,提问作者user1052610

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 05:12:38