Forms 为什么标签文本会消失

Forms 为什么标签文本会消失,forms,flutter,dart,Forms,Flutter,Dart,我正在尝试制作一个颤振应用程序,但是我在其中一个表单字段中遇到了一个奇怪的问题 它应该有一个占位符,当用户键入任何内容时,占位符将成为标签。很简单。还有一件事,表单将在完成“name”和“姓氏”字段时启用一个按钮 有两个问题正在发生。标签正在消失,按钮正在启用,就像它是一个“或”逻辑,而不是“和”逻辑。代码如下: 表单字段: Row( children: [ Container( width: 100

我正在尝试制作一个颤振应用程序,但是我在其中一个表单字段中遇到了一个奇怪的问题

它应该有一个占位符,当用户键入任何内容时,占位符将成为标签。很简单。还有一件事,表单将在完成“name”和“姓氏”字段时启用一个按钮

有两个问题正在发生。标签正在消失,按钮正在启用,就像它是一个“或”逻辑,而不是“和”逻辑。代码如下: 表单字段:

            Row(
          children: [
            Container(
              width: 100,
              height: 100,
              child: FlatButton(
                onPressed: open_gallery,
                child: Center(
                  child: _image == null
                      ? Text(
                          "add photo",
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              color: brandLightBlue,
                              fontFamily: "Roboto",
                              fontSize: 16),
                        )
                      : Text(''),
                ),
              ),
              decoration: _image == null
                  ? BoxDecoration(
                      color: brandLightGrey,
                      borderRadius: BorderRadius.circular(200))
                  : BoxDecoration(
                      image: DecorationImage(
                          image: FileImage(_image), fit: BoxFit.fill),
                      color: brandLightGrey,
                      borderRadius: BorderRadius.circular(200)),
            ),
            Container(
              width: 30,
            ),
            Column(
              children: [
                Container(
                  color: brandLightGrey,
                  width: MediaQuery.of(context).size.width * 0.6,
                  child: TextFormField(
                    onChanged: (value) {
                      print(value);
                      _inputName = value;
                    },
                    decoration: InputDecoration(
                        contentPadding: EdgeInsets.fromLTRB(10, 0, 0, 0),
                        hintText: _inputName == null || _inputName == ''
                            ? ""
                            : "Name",
                        labelText: _inputName == null || _inputName == ''
                            ? "Name"
                            : "",
                        labelStyle: TextStyle(
                            color: brandDarkGrey, fontFamily: 'Roboto'),
                        hintStyle: TextStyle(
                            color: brandDarkGrey, fontFamily: 'Roboto'),
                        fillColor: brandLightGrey),
                  ),
                ),
                Container(
                  height: 10,
                ),
                Container(
                  color: brandLightGrey,
                  width: MediaQuery.of(context).size.width * 0.6,
                  child: TextField(
                    onSubmitted: (value) {
                      _inputSurname = value;
                    },
                    decoration: InputDecoration(
                      hintText: _inputSurname == null || _inputSurname == ''
                          ? ""
                          : "Surname",
                      labelText:
                          _inputSurname == null || _inputSurname == ''
                              ? "Surname"
                              : "",
                      hintStyle: TextStyle(
                          color: brandDarkGrey, fontFamily: 'Roboto'),
                      contentPadding: EdgeInsets.fromLTRB(10, 0, 0, 0),
                    ),
                  ),
                ),
              ],
            )
          ],
        ),
下面是按钮和按钮逻辑:

        RaisedButton(
        elevation: 0,
        color: brandBlue,
        disabledColor: brandLightGrey,
        onPressed: (_inputName == null && _inputSurname == null) ||
                (_inputName == '' && _inputSurname == '')
            ? null
            : () {
                if (termAccepted == true) {}
                print("AAAA");
              },
        child: Container(
          width: MediaQuery.of(context).size.width * 0.9,
          height: MediaQuery.of(context).size.height * 0.08,
          child: Center(
              child: Text(
            "Next",
            style: TextStyle(
                color: brandWhite, fontFamily: 'Alata', fontSize: 18),
          )),
        )),

我建议您对
TextField
使用控制器

final _inputSurnameController = TextEditingController();
final _inputNameController = TextEditingController();
 TextField(
        onSubmitted: (value) {
            _inputSurname = value;
        },
      decoration: InputDecoration(
          hintText: _inputSurnameController.text == "" ? "": "Surname",
          labelText: _inputSurnameController.text == ""? "Surname": "",
           hintStyle: TextStyle(
                color: brandDarkGrey, fontFamily: 'Roboto'),
                contentPadding: EdgeInsets.fromLTRB(10, 0, 0, 0),
           ),
   ),
将控制器添加到
文本字段中

final _inputSurnameController = TextEditingController();
final _inputNameController = TextEditingController();
 TextField(
        onSubmitted: (value) {
            _inputSurname = value;
        },
      decoration: InputDecoration(
          hintText: _inputSurnameController.text == "" ? "": "Surname",
          labelText: _inputSurnameController.text == ""? "Surname": "",
           hintStyle: TextStyle(
                color: brandDarkGrey, fontFamily: 'Roboto'),
                contentPadding: EdgeInsets.fromLTRB(10, 0, 0, 0),
           ),
   ),

把你按下的按钮改成按下:(\u inputName==null&&u inputnasname==null)| |(\u inputName==''和&&u inputnasname=='')
?它还能按吗?是的,它仍然有相同的结果!