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

Flutter:如何修改文本选中工具栏的选项文字颜色

Flutter TextField选中工具栏文字颜色修复方案

全局主题配置(推荐)

在App的ThemeData中,通过textSelectionTheme的toolbarTextStyle指定工具栏选项的文字颜色,同时可配套设置背景色保证对比度:

ThemeData(
  textSelectionTheme: TextSelectionThemeData(
    toolbarTextStyle: TextStyle(
      color: Colors.blue, // 自定义文字颜色,确保与背景色有足够对比度
      fontSize: 16,
    ),
    // 可选:同步设置工具栏背景色
    toolbarBackgroundColor: Colors.grey[200],
  ),
)

如果需要适配明暗主题,可根据当前主题亮度动态切换颜色:

ThemeData(
  textSelectionTheme: TextSelectionThemeData(
    toolbarTextStyle: TextStyle(
      color: Theme.of(context).brightness == Brightness.light 
          ? Colors.black87 
          : Colors.white70,
      fontSize: 16,
    ),
    toolbarBackgroundColor: Theme.of(context).brightness == Brightness.light 
        ? Colors.grey[200] 
        : Colors.grey[800],
  ),
)

单个TextField自定义工具栏

若仅需给特定TextField单独配置,可通过toolbarBuilder完全自定义工具栏样式:

TextField(
  toolbarOptions: ToolbarOptions(
    copy: true,
    cut: true,
    paste: true,
    selectAll: true,
  ),
  toolbarBuilder: (context, editableTextState) {
    final isLight = Theme.of(context).brightness == Brightness.light;
    return Container(
      color: isLight ? Colors.grey[200] : Colors.grey[800],
      padding: const EdgeInsets.symmetric(vertical: 8),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          TextButton(
            onPressed: editableTextState.cutSelection,
            child: Text('剪切', style: TextStyle(color: isLight ? Colors.black87 : Colors.white70)),
          ),
          TextButton(
            onPressed: editableTextState.copySelection,
            child: Text('复制', style: TextStyle(color: isLight ? Colors.black87 : Colors.white70)),
          ),
          TextButton(
            onPressed: editableTextState.pasteText,
            child: Text('粘贴', style: TextStyle(color: isLight ? Colors.black87 : Colors.white70)),
          ),
          TextButton(
            onPressed: editableTextState.selectAll,
            child: Text('全选', style: TextStyle(color: isLight ? Colors.black87 : Colors.white70)),
          ),
        ],
      ),
    );
  },
)

注意:你之前尝试的cursorColor仅控制文本光标颜色,与工具栏文字样式无关,需通过上述toolbarTextStyle或自定义工具栏来修改选项文字颜色。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 21:37:24