Как отобразить текст счетчика в TextFormField () только в том случае, если он сфокусирован на флаттере? - PullRequest
0 голосов
/ 27 марта 2020

У меня есть виджет TextFormField(), я хочу отобразить CounterText, но только когда поле сфокусировано

и еще один побочный вопрос Как изменить цвет фона поля при фокусировке?

enter image description here

код: -

TextFormField(
  decoration: InputDecoration(
    labelText: "Weight",
    border: OutlineInputBorder(
    borderSide: BorderSide(
    color:Colors.grey[200],
    width: 1,
    style: BorderStyle.solid
  ),
  gapPadding: 4.0
 ),
  counterText:"Enter Your Weight",
),
),

1 Ответ

1 голос
/ 27 марта 2020

Я думаю, что самый простой способ - создать список Boolean и обновить его так, чтобы вы знали, когда выбрана TextForm, а затем отображать counterText

Column(
      children: <Widget>[
        SizedBox(height: 50),
        TextFormField(
          onTap: () {
            setState(() {
              selected[0] = true;
              selected[1] = false;
            });
          },
          controller: textController,
          decoration: InputDecoration(
            labelText: "Weight",
            border: OutlineInputBorder(
                borderSide: BorderSide(
                    color: Colors.grey[200],
                    width: 1,
                    style: BorderStyle.solid),
                gapPadding: 4.0),
            counterText: selected[0] ? "Enter Your Weight" : ' ',
          ),
        ),
        TextFormField(
          onTap: () {
            setState(() {
              selected[0] = false;
              selected[1] = true;
            });
          },
          decoration: InputDecoration(
            labelText: "Age",
            border: OutlineInputBorder(
                borderSide: BorderSide(
                    color: Colors.grey[200],
                    width: 1,
                    style: BorderStyle.solid),
                gapPadding: 4.0),
            counterText: selected[1] ? "Enter Your Age" : ' ',
          ),
        )
      ],
    )

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...