我有一个用于密码字段的TextField小部件和一个用于显示/隐藏密码的IconButton。我想要的是,当用户点击IconButton时,它应该更改颜色。实际上,当我在单击IconButton之前和之后运行print(showPassWordIconColor)
时,它的值会更改。但是,它不会显示更改的颜色。
我看到了其他一些问题及其答案,并尝试了它们,但仍然遇到相同的问题。
这是完整的小部件。
(最初是showPasswordIconColor = Colors.grey)
Widget passwordField = AppTextFormField(
obscureText: !_showPassword,
decoration: InputDecoration(
hintText: "Password",
border: OutlineInputBorder(),
suffixIcon: IconButton(
icon: Icon(
Icons.remove_red_eye,
color: showPasswordIconColor,
),
onPressed: () {
setState(() {
_showPassword = !_showPassword;
if (showPaswswordIconColor == Colors.grey) {
showPaswswordIconColor = buttonColor;
} else {
showPaswswordIconColor = Colors.grey;
}
print(showPaswswordIconColor);
});
},
),
),
);
答案 0 :(得分:0)
然后使用下面的代码。
Container(
width: 200,
height: 200,
child: TextFormField(
obscureText: !_showPassword,
decoration: InputDecoration(
hintText: "Password",
border: OutlineInputBorder(),
suffixIcon: IconButton(
icon: Icon(
Icons.remove_red_eye,
color: showPasswordIconColor,
),
onPressed: () {
setState(() {
_showPassword = !_showPassword;
if (showPasswordIconColor == Colors.grey) {
showPasswordIconColor = Colors.red;
} else {
showPasswordIconColor = Colors.grey;
}
print(showPasswordIconColor);
});
},
),
),
),
)