Флаттер почему текст метки исчезает

#forms #flutter #dart

#формы #флаттер #dart

Вопрос:

Я пытаюсь создать приложение flutter, однако я столкнулся со странной проблемой в одном из полей формы.

Предполагается, что у него должен быть заполнитель, и когда пользователь вводит что-либо, заполнитель становится меткой. Достаточно просто. существует также то, что форма включит кнопку, когда поля «имя» и «фамилия» будут заполнены.

Возникают две проблемы. Метка исчезает, и кнопка включается, как будто это была логика «Или», а не «И» логика. Вот коды: Поле формы:

             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 amp;amp; _inputSurname == null) ||
                (_inputName == '' amp;amp; _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),
          )),
        )),
  

Комментарии:

1. Как насчет того, чтобы изменить нажатую кнопку на onPressed: (_inputName == null amp;amp; _inputSurname == null) || (_inputName == ''" amp;amp; _inputSurname == "'') ? Он все еще мог нажимать?

2. да, он по-прежнему имеет тот же результат!

Ответ №1:

Я предложу вам использовать контроллер для вашего TextField .

 final _inputSurnameController = TextEditingController();
final _inputNameController = TextEditingController();
  

Добавьте контроллер к вашему TextField .

  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),
           ),
   ),