如何在小部件中显示/隐藏密码?

huangapple go评论59阅读模式
英文:

How to show/hide password in Widget?

问题

我是中文翻译,以下是您要翻译的内容:

i'm new to flutter and I want a button like interaction which will make password visible and invisible.Can I do it inside TextFormField?

child:TextField(
              obscureText: true,
              style: TextStyle(
                  color:Colors.black87
              ),
              decoration: InputDecoration(
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.only(top:14),
                  prefixIcon: Icon(
                      Icons.lock,
                      color:Color(0xff992a32)
                  ),
                  hintText: 'Şifre giriniz.',
                  hintStyle: TextStyle(
                      color:Colors.black38
                  )
              ),
            )

这段代码中,您想要实现在TextField内部点击按钮以显示或隐藏密码的交互效果。可以尝试使用obscureText属性来实现密码的可见和不可见切换。

英文:

i'm new to flutter and I want a button like interaction which will make password visible and invisible.Can I do it inside TextFormField?

child:TextField(
          obscureText: true,
          style: TextStyle(
              color:Colors.black87
          ),
          decoration: InputDecoration(
              border: InputBorder.none,
              contentPadding: EdgeInsets.only(top:14),
              prefixIcon: Icon(
                  Icons.lock,
                  color:Color(0xff992a32)
              ),
              hintText: 'Şifre giriniz.',
              hintStyle: TextStyle(
                  color:Colors.black38
              )
          ),
        )

答案1

得分: 0

创建布尔变量isVisible

bool isVisible = false;

设置TextFormField如下所示:

TextFormField(
  obscureText: isVisible,
  style: const TextStyle(color: Colors.black87),
  decoration: InputDecoration(
    border: InputBorder.none,
    contentPadding: const EdgeInsets.only(top: 14),
    prefixIcon: const Icon(Icons.lock, color: Color(0xff992a32)),
    suffixIcon: IconButton(
      onPressed: () {
        setState(() {
          isVisible = !isVisible;
        });
      },
      icon: Icon(
        !isVisible ? Icons.visibility : Icons.visibility_off,
        color: Theme.of(context).primaryColorDark,
      ),
    ),
  ),
)
英文:

Create bool variable isVisible:

bool isVisible = false;

setup TextFormField like this:

 TextFormField(
        obscureText: isVisible ,
        style: const TextStyle(color: Colors.black87),
        decoration: InputDecoration(
          border: InputBorder.none,
          contentPadding:const EdgeInsets.only(top: 14),
          prefixIcon:const Icon(Icons.lock, color: Color(0xff992a32)),
          suffixIcon: IconButton(
            onPressed: () {
              setState(() {
                isVisible = !isVisible;
              });
            },
            icon: Icon(
              !isVisible ? Icons.visibility : Icons.visibility_off,
              color: Theme.of(context).primaryColorDark,
            ),
          ),
        ),
      ),

huangapple
  • 本文由 发表于 2023年2月6日 19:51:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定