首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IconButton颜色不变(颤动)

IconButton颜色不变(颤动)
EN

Stack Overflow用户
提问于 2020-09-09 17:28:53
回答 1查看 271关注 0票数 0

我有一个用于密码字段的TextField小部件和一个用于显示/隐藏密码的IconButton。我想要的是当用户点击IconButton时,它应该会改变颜色。实际上,当我在单击IconButton之前和之后运行print(showPassWordIconColor)时,它的值会发生变化。但是,它不会显示更改后的颜色。我已经看到了其他一些问题和他们的答案,我尝试了他们,但我仍然得到相同的问题。这是完整的小部件。(初始showPasswordIconColor = Colors.grey)

代码语言:javascript
复制
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);
        });
      },
    ),
  ),
);
EN

回答 1

Stack Overflow用户

发布于 2020-09-09 17:42:04

请检查以下代码。

然后使用下面的代码。

代码语言:javascript
复制
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);
                    });
                  },
                ),
              ),
            ),
          )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63808725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档