仅当焦点对准时,如何在TextFormField()上显示计数器文本?

时间:2020-03-27 14:19:58

标签: forms flutter

我有一个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 个答案:

答案 0 :(得分:1)

我认为最简单的方法是创建一个布尔列表并对其进行更新,这样您就知道何时选择了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