为什么我的文本字段在按下按钮后清空,该按钮会在Flutter中显示模糊文本?

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

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: &lt;Widget&gt;[

            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: &quot;Enter your email&quot;,
                fillColor: setupMainTheme,
              ),
            ),

            SizedBox(height: 18),

            TextFormField(
              controller: _passController,
              obscuringCharacter: &#39;*&#39;,
              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: &quot;Enter your password&quot;,
                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)=&gt;setState(() {
                              this.password = value;
                            }),

huangapple
  • 本文由 发表于 2023年3月3日 21:24:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627640.html
匿名

发表评论

匿名网友

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

确定