flutter - Flutter:如何通过 ThemeData 更改 TextFormField prefixIcon 不聚焦颜色?
问题描述
Flutter:我可以通过设置 Theme.accentColor 来更改 TextFormField 的 prefixIcon 焦点颜色,但是在没有焦点时找不到任何方法来更改 prefixIcon 颜色。
解决方案
Use FocusNode Class, this will keep a track of when you have the focus on that particular TextFormField
.
Note: Make sure you assign different FocusNode
object to different TextFormField()
if you have multiple class.
Here is your solution:
FocusNode _focusNode;
@override
void dispose() {
super.dispose();
_focusNode.dispose();
}
@override
void initState() {
super.initState();
_focusNode = new FocusNode();
_focusNode.addListener(_onOnFocusNodeEvent);
}
_onOnFocusNodeEvent() {
setState(() {
// Re-renders
});
}
TextFormField(
focusNode: _focusNode
decoration: InputDecoration(
border: InputBorder.none,
prefixIcon: Padding(
padding: EdgeInsets.all(0.0),
child: Icon(
Icons.search,
color: this.getPrefixIconColor(),
) // icon is 48px widget.
)
)
)
//This will change the color of the icon based upon the focus on the field
Color getPrefixIconColor(){
return _focusNode.hasFocus ? Colors.black : Colors.grey
}
Let me know if that helps. Thanks
推荐阅读
- java - java - 当元素重复时,如何根据另一个数组对一个数组进行排序?
- xamarin.forms - Guage 控件落后于其他控件
- javascript - 添加数组的连续元素
- flutter - 来自自己的流的每个项目的颤振块产量
- javascript - 无法使用 discord.js 获取带有 giphy 的 gif,制作机器人
- azure - Stop-AzureRmWebApp - 查找 Dll
- python - 在 Python 中使用 Pandas 清理我的数据
- python-3.x - 刚切换到 TensorFlow 2.1 并收到一些烦人的警告
- swift - 检索自定义类时 NSKeyedUnarchiver 返回 nil
- python - 输入包含对于 dtype“float64”来说太大的无穷大值