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

Flutter如何修改TextField未聚焦时的下划线颜色?

解决TextField未聚焦时下划线颜色修改问题

嘿,这个问题我之前折腾过一阵!确实默认的InputDecorationTheme里没那么直观地显示未聚焦下划线的设置项,但其实只要找准对应的border属性就能搞定——未激活/未聚焦状态的TextField对应的是enabledBorder,咱们直接配置它就行。

方法一:全局Theme统一配置(推荐)

如果你想让所有TextField都应用这个样式,直接在InputDecorationTheme里添加enabledBorder的配置就可以,同时也可以明确设置聚焦时的focusedBorder来保持样式统一:

inputDecorationTheme: InputDecorationTheme(
  labelStyle: TextStyle(color: Colors.blue[200]),
  hintStyle: TextStyle(color: Colors.grey),
  // 未聚焦/未激活时的下划线样式
  enabledBorder: UnderlineInputBorder(
    borderSide: BorderSide(color: Colors.grey[300]), // 你要的浅灰色
  ),
  // 可选:设置聚焦时的下划线颜色(如果需要自定义的话)
  focusedBorder: UnderlineInputBorder(
    borderSide: BorderSide(color: Colors.blue[200]),
  ),
),

方法二:单个TextField单独配置

如果只是想给某个特定的TextField修改样式,直接在它的InputDecoration里配置enabledBorder即可:

TextField(
  decoration: InputDecoration(
    labelText: "请输入内容",
    labelStyle: TextStyle(color: Colors.blue[200]),
    hintStyle: TextStyle(color: Colors.grey),
    enabledBorder: UnderlineInputBorder(
      borderSide: BorderSide(color: Colors.grey[300]),
    ),
    focusedBorder: UnderlineInputBorder(
      borderSide: BorderSide(color: Colors.blue[200]),
    ),
  ),
)

额外补充:如果你的TextField会用到禁用状态(enabled: false),还可以通过disabledBorder属性来设置禁用时的下划线样式哦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:29:59