颤动,两个不同的文本字段具有相同的值

时间:2020-10-27 21:20:23

标签: flutter

我正在Flutter上进行注册和登录。 因为我知道我会使用多个文本字段,所以我制作了一个小部件函数来按需创建一个。像这样:

  Widget buildInput({bool obscureText, icon, labelText, Function change}) {
Color borderCo = HexColor('#88d5cb');

return Container(
  padding: EdgeInsets.only(top: 35, left: 20, right: 20),
  child: Column(
    children: <Widget>[
      TextField(
        onChanged: change,
        obscureText: obscureText,
        decoration: InputDecoration(
          prefixIcon: Icon(icon),
            border: UnderlineInputBorder(
                borderSide: BorderSide(color: borderCo)),
            focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.grey)),
            enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: borderCo)),
            labelText: labelText,
            labelStyle: TextStyle(
              fontFamily: 'Arial',
              fontWeight: FontWeight.bold,
              color: Colors.grey,
            )
        ),
      )
    ],
  )
);

所以每次我需要输入时,我都会这样称呼它

buildInput(
        obscureText: false, 
        icon: Icons.mail, 
        labelText:'EMAIL', 
        change: (login) => setState(() => _loginEmail = login)
      ),

问题是我有一个带有两个按钮的屏幕,一个用于登录,一个用于注册,而默认情况下选择了登录按钮,而登录小部件显示为默认。 如果用户要注册,请单击正确的按钮,然后渲染我为注册所做的其他小部件功能。 这是loginWidget函数:

Widget loginWidget(size) {
return Container(
  child: Column(
    children: <Widget>[
      buildInput(
        obscureText: false, 
        icon: Icons.mail, 
        labelText:'EMAIL', 
        change: (login) => setState(() => _loginEmail = login)
      ),
      buildInput(
        obscureText:true, 
        icon:Icons.lock, 
        labelText:'PASSWORD',
        change: (login) => setState(() => _loginPassword = login)
      ),
      SizedBox(height: 15,),
      SizedBox(
        width: MediaQuery.of(context).size.width * 0.9,
        child: RaisedButton(
          disabledColor: Colors.grey[300],
          elevation: 8,
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 10),
            child: Text(
              'LOGIN',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: HexColor('#88d5cb')),
            ),
          ),
          color: Colors.white,
          shape: RoundedRectangleBorder(
            side: BorderSide(color: HexColor('#88d5cb')),
            borderRadius: BorderRadius.circular(10.0),
          ),
          onPressed: (EmailValidator.validate(_loginEmail) && _loginPassword.length >= 6) ?() async {
              try {
                Dialogs.showLoadingSpinner(context);
                bool isLoggedIn = await HttpServices.login(emailPass: { 
                  'email': _loginEmail, 
                  'password': _loginPassword 
                });

                if(isLoggedIn) { 
                  Dialogs.hideLoadingSpinner(context);
                  Navigator.of(context).pushReplacement(
                    MaterialPageRoute(
                      builder: (BuildContext context) => CollabityHome()
                    )
                  );
                } else {
                  Dialogs.hideLoadingSpinner(context);
                  // Dialogs.showAlert(context, 'Email or password incorrect, try again.');
                  setState(() {
                    err = "The email or password you've entered doesn't match any account.";
                  });
                  print(err);
                }
              } catch(ex) {
                print('login ex: $ex');
              }
            }
            :
            null
        )
      ),
    ],
  )
);

}

我的大问题是,例如当我在登录的电子邮件输入中键入内容时,然后单击“注册”按钮以查看注册小部件功能时,注册的输入具有登录输入的值匹配他们在屏幕上出现的位置的人。

这是我在构建用于登录和注册的小部件之间切换的方式

swap? loginWidget(size) : signupWidget(size),

其中的按钮使用setstate()更改swap的布尔值。

1 个答案:

答案 0 :(得分:0)

我建议您为每个输入创建一个TextEditingController,以便可以将其作为参数传递给buildInput()函数。这样,更容易处理值而不会造成混淆。

相关问题