[英]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.