0

我有一个用于密码字段的 TextField 小部件和一个用于显示/隐藏密码的 IconButton。我想要的是当用户点击 IconButton 时,它应该改变颜色。实际上,当我print(showPassWordIconColor)在单击 IconButton 之前和之后运行时,它的值会发生变化。但是它不显示更改后的颜色。我看过其他一些问题和他们的答案,我试过了,但我仍然遇到同样的问题。这是完整的小部件。(最初 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);
        });
      },
    ),
  ),
);
4

1 回答 1

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);
                    });
                  },
                ),
              ),
            ),
          )
于 2020-09-09T09:42:04.243 回答