如何在Flutter中隐藏TextField底部的字母计数器

182

在此输入图像描述

我遇到了一个小问题。如您所见,在Flutter中我将TextField的maxLength设置为1,但我无法隐藏文本计数器的底部标签。

22个回答

0

正如最佳答案中@user10481267所提到的,最好使用buildCounter属性。这样可以极大地提高灵活性,甚至可以动态决定是否显示计数器。

我正在使用JSON构建一个动态表单,并包含必需的属性。我的实现如下:

TextFormField(
    buildCounter: (BuildContext context,
    {
            int currentLength,
            int maxLength,
            bool isFocused}) {
                    if (isFocused)
                            return formFields[index]["max"] == null
                                    ? null
                                    : Text(
                                        '$currentLength / $maxLength',
                                        semanticsLabel: 'character count',
                                      );
                              else
                                return null;
                            },
                            maxLength: formFields[index]["max"] ?? 100,
                            decoration: new InputDecoration(
                              labelText: formFields[index]["hint"] ?? "",
                              fillColor: Colors.green,
                              border: new OutlineInputBorder(
                                borderRadius: new BorderRadius.circular(15.0),
                                borderSide: new BorderSide(),
                              ),
                            )
                          )

0

使用高度和宽度均为零的SizedBox:

TextField(
          maxLength: 400,
          decoration: InputDecoration(
            counter: SizedBox(
              width: 0,
              height: 0,
            ),),)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接