简体   繁体   English

Flutter web 当设置为显示密码时,应用程序不显示输入的密码

[英]Flutter web app not showing typed password when set to show password

On my flutter webapp I am trying to show the password if the user chose to show it.在我的 flutter webapp 上,如果用户选择显示密码,我会尝试显示密码。 If I set my bool value to false it start by showing the normal text and then if I press the button to show or hide the password it will obscure the text but it will not un-obscure the text.如果我将我的 bool 值设置为 false,它首先显示普通文本,然后如果我按下按钮显示或隐藏密码,它会隐藏文本,但不会取消隐藏文本。 I added a print function in one of my try's and it does change the bool value from false to true and back to false.我在我的一个尝试中添加了打印 function,它确实将 bool 值从 false 更改为 true,然后再更改为 false。 I have it it a setState but no luck.我有一个 setState 但没有运气。 I tryied many different examples online but can not get it to work with the web app.我在网上尝试了许多不同的示例,但无法使其与 web 应用程序一起使用。

The code代码

@override
Widget build(BuildContext context) {
c_width = MediaQuery.of(context).size.width*0.8;
return Scaffold(
appBar: AppBar(title: Text("App"),
),
body: signUpWidget(widget.signUpValue),
);
}

Widget signUpWidget(String? rl)  {
switch(rl) {
case 'User': {
return SingleChildScrollView(
child: Container (
width: c_width,
padding: const EdgeInsets.all(16.0),

child: Column(
child: Column(
children: <Widget>[



TextFormField(
obscureText: isObscure,
decoration: InputDecoration(
suffix: TextButton(
child: isPasswordObscure
? Text(
'Show',
style: TextStyle(color: Colors.grey),
)

: Text(
'Hide',
style: TextStyle(color: Colors.grey),
),
onPressed: () {
setState(() { 
isObscure = !isObscure; 
isPasswordObscure = !isObscure;
});
},
),
),

), ),

Like I say it changes the text to hide and show and if I print the bool value it changes from true to false, but it does not unObscure the text in the field to show the password.就像我说的那样,它会将文本更改为隐藏和显示,如果我打印 bool 值,它会从 true 变为 false,但它不会取消隐藏字段中的文本以显示密码。 Is it something to do with web?跟web有关系吗? or am I missing something?还是我错过了什么?

Thank you.谢谢你。

Actually you dont need to have two bool,实际上你不需要有两个布尔值,

TextFormField(
  obscureText: isObscure,
  decoration: InputDecoration(
    suffix: TextButton(
      child: isObscure //< using 
          ? Text(
              'Show',
              style: TextStyle(color: Colors.grey),
            )
          : Text(
              'Hide',
              style: TextStyle(color: Colors.grey),
            ),
      onPressed: () {
        setState(() {
          isObscure = !isObscure;
          // isPasswordObscure = !isObscure; // here it reverse the variable isPasswordObscure 
        });
      },
    ),
  ),
)

It might be occurring due to this problem in setState((){}) .它可能是由于setState((){})中的这个问题而发生的。

setState(() { 
 // assume isObscure is true initially
 isObscure = !isObscure; 
 // now isObscure is false
 isPasswordObscure = !isObscure;
 // now isPasswordObscure is true, because isObscure's value has already changed.
});

To solve this, first save isObscure to a temporary variable.要解决这个问题,首先将 isObscure 保存到一个临时变量中。

setState(() { 
 var temp = isObscure;
 isObscure = !temp; 
 isPasswordObscure = !temp;
});

Or,或者,

Entirely avoid using two bools.完全避免使用两个布尔值。

setState(() { 
 isObscure = !isObscure; 
});

It seems like this occurs only in web.这似乎只发生在 web 中。

Follow these steps to fix this,请按照以下步骤解决此问题,

https://github.com/flutter/flutter/issues/83695#issuecomment-1083537482 https://github.com/flutter/flutter/issues/83695#issuecomment-1083537482

Or或者

Upgrade to the latest Flutter version.升级到最新的Flutter版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户单击 flutter 应用程序中的后缀图标时,如何一一显示密码并确认密码? - how to show password and confirm password one by one when user click on suffix icon in flutter app? 在 Flutter 应用程序中显示自动强密码 iOS - Show Automatic Strong password iOS in Flutter app Flutter - 单击显示密码图标时没有变化 - Flutter - No change when clicking show password icon 密码不匹配时颤动显示错误 - Flutter show error when Password not match 如何在Flutter Web应用程序中实现电子邮件/密码身份验证? - How to implement email/password authentication in a flutter web app? 如何在 flutter 中隐藏/显示密码 - How to hide/Show Password in flutter 如何要求用户通过Flutter在应用程序中设置电话密码? - How to require user to set phone password in app by Flutter? 为什么在 flutter 中使用堆栈小部件时显示/隐藏密码不起作用 - why show/hide password not working when use stack widget in flutter 尝试使用电子邮件和密码创建帐户时,Flutter 应用程序崩溃 - Flutter app is crashing when trying to creating account with email and password 密码显示/隐藏切换删除密码 TextField 值 (Flutter) - Password show/hide toggle deletes password TextField value (Flutter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM