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
相关产品推荐
相关产品推荐

