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

Flutter中DropdownMenu下拉菜单文本颜色自定义方法

修改Flutter DropdownMenu下拉菜单文本颜色

要将DropdownMenu下拉选项的文本颜色设置为Colors.white,可以通过以下两种方式实现:

方式一:直接在DropdownMenuEntry中配置样式

你已经在使用MenuItemButton.styleFrom配置条目样式,只需将foregroundColor改为Colors.white即可。同时为了让白色文本清晰显示,建议给下拉菜单添加深色背景:

DropdownMenu<CurrencyPair>(
  width: 300,
  initialSelection: CurrencyPair.getById(store.currencyId),
  controller: TextEditingController(),
  requestFocusOnTap: false,
  onSelected: (CurrencyPair? currency) {
    store.currencyId = currency?.id ?? 0;
    store.setCurrencyIdByDefault(store.currencyId);
  },
  label: const Text('Currency pair'),
  // 添加菜单样式,设置背景色以适配白色文本
  menuStyle: MenuStyle(
    backgroundColor: MaterialStateProperty.all(const Color.fromARGB(255, 3, 1, 75)),
  ),
  dropdownMenuEntries: CurrencyPair.values
      .map<DropdownMenuEntry<CurrencyPair>>(
          (CurrencyPair currency) {
        return DropdownMenuEntry<CurrencyPair>(
          value: currency,
          label: currency.name,
          style: MenuItemButton.styleFrom(
            // 直接设置文本颜色为白色
            foregroundColor: Colors.white,
          ),
        );
      }).toList(),
),

方式二:自定义DropdownMenuEntry的label组件

如果需要更精细的文本样式控制,可以直接传入自定义Text组件作为label,手动设置文本颜色:

DropdownMenuEntry<CurrencyPair>(
  value: currency,
  // 自定义label文本样式
  label: Text(
    currency.name,
    style: const TextStyle(color: Colors.white),
  ),
  style: MenuItemButton.styleFrom(
    // 若有需要,可同时设置选中/hover状态的前景色
    foregroundColor: Colors.white,
  ),
),

注意:如果下拉菜单背景是浅色,设置白色文本会导致可读性问题,务必配合menuStyle调整菜单背景色。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 06:59:53