[英]Flutter SvgPicture in TextField not adapting color
When I set the TextField.prefixIcon
property to an Icon(Icons.example)
widget, the color of the icon automatically adapts according to the state of the TextField (enabled, focussed, ...).当我将
TextField.prefixIcon
属性设置为Icon(Icons.example)
小部件时,图标的颜色会根据 TextField 的 state(启用、聚焦等)自动调整。 Now, I got an Icon Pack with svg files that I want to use within the TextField.现在,我得到了一个包含 svg 个文件的图标包,我想在 TextField 中使用这些文件。 I am using flutter_svg to show them like that:
我正在使用flutter_svg来展示它们:
TextFormField(
decoration: InputDecoration(
hintText: "Password",
prefixIcon: SvgPicture.asset(
AppIcons.lock,
),
),
),
Now, how can I achieve the automatic color adaption with SvgPicture?那么,如何实现SvgPicture的自动颜色适配呢?
you need to resolve it manually:您需要手动解决它:
FocusNode _focusNode = FocusNode();
bool _enabled = true;
@override
Widget build(BuildContext context) {
return TextFormField(
focusNode: _focusNode,
enabled: _enabled,
decoration: InputDecoration(
hintText: "password"
).copyWith(
prefixIcon: SvgPicture.asset(AppIcons.lock, color: _resolveColor())
),
);
}
Color _resolveColor(){
if (_focusNode.hasFocus){
return Colors.yellow;
}
if(!_enabled){
return Colors.white;
}
if (!_form.valid){
return Colors.red;
}
return Colors.grey;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.