英文:
Why does my text fields gets cleared when I pressed a button that will reveal obscure text in Flutter?
问题
我尝试创建一个登录页面作为一个副项目,它包含两个文本字段,电子邮件地址和密码。在密码字段上,有一个按钮,点击时会显示明文文本,第二次点击会重新隐藏密码文本,依此类推。
现在的主要问题是:点击时不是显示密码文本,而是清除了两个文本字段。
这是代码:
bool _visiblePassword = false;
@override
void initState() {
_visiblePassword = false;
}
final _emailController = TextEditingController();
final _passController = TextEditingController();
body: Padding(
padding: EdgeInsets.all(28),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
controller: _emailController,
style: TextStyle(color: Color.fromARGB(255, 214, 213, 213)),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
filled: true,
hintStyle: TextStyle(color: Color fromARGB(255, 197, 197, 197)),
hintText: "输入您的电子邮件",
fillColor: setupMainTheme,
),
),
SizedBox(height: 18),
TextFormField(
controller: _passController,
obscuringCharacter: '*',
obscureText: !_visiblePassword,
style: TextStyle(color: Color.fromARGB(255, 214, 213, 213)),
decoration: InputDecoration(
suffixIcon: IconButton(
icon: Icon(
_visiblePassword ? Icons.visibility : Icons.visibility_off,
color: Theme.of(context).primaryColorDark,
),
onPressed: () {
setState(() {
_visiblePassword = !_visiblePassword;
});
},
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
filled: true,
hintStyle: TextStyle(color: Color.fromARGB(255, 197, 197, 197)),
hintText: "输入您的密码",
fillColor: setupMainTheme,
),
),
],
),
),
这是代码的翻译部分。如果有任何问题,请随时提出。
英文:
I'm trying to make a a sign-in page as side project, it contains two fields of text, Email address & Password, on the password field it has a button that will reveals the obscure text on click and re-obscure the password text on second click and so on.
Now the main problem is: Instead of revealing the password text on click, both text field gets cleared instead.
This is the code:
bool _visiblePassword = false;
@override
void initState() {
_visiblePassword = false;
}
final _emailController = TextEditingController();
final _passController = TextEditingController();
body: Padding(
padding: EdgeInsets.all(28),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
controller: _emailController,
style: TextStyle(color: Color.fromARGB(255, 214, 213, 213)),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
filled: true,
hintStyle: TextStyle(color: Color.fromARGB(255, 197, 197, 197)),
hintText: "Enter your email",
fillColor: setupMainTheme,
),
),
SizedBox(height: 18),
TextFormField(
controller: _passController,
obscuringCharacter: '*',
obscureText: !_visiblePassword,
style: TextStyle(color: Color.fromARGB(255, 214, 213, 213)),
decoration: InputDecoration(
suffixIcon: IconButton(
icon: Icon(
_visiblePassword ? Icons.visibility : Icons.visibility_off,
color: Theme.of(context).primaryColorDark,
),
onPressed: () {
setState(() {
_visiblePassword = !_visiblePassword;
});
},
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
filled: true,
hintStyle: TextStyle(color: Color.fromARGB(255, 197, 197, 197)),
hintText: "Enter your password",
fillColor: setupMainTheme,
),
),
答案1
得分: 3
如果您在build
方法内定义了_emailController
和_passController
,请将它们移到外部。这样,在调用setState
时,它们不会被重新初始化。
英文:
If you have _emailController
and _passController
defined inside the build
method, then move them outside it. This is so they don't get reinitialized when setState
is called.
答案2
得分: 0
String password = '';
然后,不要使用控制器,而是使用以下方式:
onChanged: (value) => setState(() {
this.password = value;
}),
英文:
first declare a password variable before the build state
String password = ''
then instead of using controller use
onChanged: (value)=>setState(() {
this.password = value;
}),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论