我无法以某种方式显示在文本表单字段中输入的信息。我的代码有问题吗?

问题描述 投票:1回答:1

如果可以捕获并打印输入到文本表单字段中的数据,我正在测试我的登录页代码。但是,它无法同时显示电子邮件和密码文本表单字段数据。相反,它只能从最近编辑/输入的文本表单字段中打印出数据。另一个将不显示任何内容。我的代码似乎也找不到任何问题。请帮帮我。

这是我的输入和输出结果:SignInInputSignInOutput

使用几乎类似的代码,我的注册页面效果很好:SignUpInputSignUpOutput

下面是我的登录页面的代码:


class SignIn extends StatefulWidget {
  @override
  _SignInState createState() => _SignInState();
}

class _SignInState extends State<SignIn> {

  //Text field state
  String email = '';
  String password = '';

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        body: Center(
          child: Form(
            child: ListView(
              physics: NeverScrollableScrollPhysics(),
              children: <Widget>[
                Container(
                  padding: EdgeInsets.only(
                    left: 30.0,
                    right: 30.0,
                    top: 10.0,
                    bottom: 10.0,
                  ),
                  child: TextFormField(
                    onChanged: (val) {
                      setState(() => email = val);
                    },
                    decoration: const InputDecoration(
                      icon: Icon(Icons.email),
                      labelText: 'Email',
                    ),
                  ),
                ),

                Container(
                  padding: EdgeInsets.only(
                    left: 30.0,
                    right: 30.0,
                    top: 10.0,
                    bottom: 10.0,
                  ),
                  child: TextFormField(
                    onChanged: (val) {
                      setState(() => email = val);
                    },
                    decoration: const InputDecoration(
                      icon: Icon(Icons.lock),
                      labelText: 'Password',
                    ),
                  ),
                ),

                Container(
                    padding: EdgeInsets.only(
                      left: 10.0,
                      right: 10.0,
                      top: 10.0,
                      bottom: 10.0,
                    ),
                    child: CheckboxListTile(
                      value: true,
                      title: Text("Keep me signed in to receive chat messages!"),
                      controlAffinity: ListTileControlAffinity.leading,
                    )
                ),

                Container(
                  padding: EdgeInsets.only(
                    left: 30.0,
                    right: 30.0,
                    top: 10.0,
                    bottom: 10.0,
                  ),
                  child: InkWell(
                    child: FlatButton(
                      padding: EdgeInsets.only(
                        top: 12.0,
                        bottom: 12.0,
                      ),
                      color: Colors.redAccent,
                      textColor: Colors.white,
                      onPressed: () async {
                        print(email);
                        print(password);
                      },
                      child: Text('Sign In'),
                    ),
                  ),
                ),

                Container(
                  padding: EdgeInsets.only(
                    top: 10.0,
                    bottom: 10.0,
                  ),
                  child: Center(
                    child: InkWell(
                      child: Text(
                        'Forgot Password',
                        style: TextStyle(
                          color: Colors.redAccent,
                        ),
                      ),
                      onTap: (){},
                    ),
                  ),
                ),

              ],
            ),
          ),
        )
    );
  }
}



flutter
1个回答
0
投票

您仅将已更改的文本从TextFormField分配给email属性

//...
                  child: TextFormField(
                    onChanged: (val) {
                      setState(() => email = val);
                    },
//...
                  child: TextFormField(
                    onChanged: (val) {
                      setState(() => email = val);
                    },
//...

所以只需将第二个更改为:

                  child: TextFormField(
                    onChanged: (val) {
                      setState(() => password = val);
                    },
© www.soinside.com 2019 - 2024. All rights reserved.