在使用 maxLength
属性时,想要隐藏 TextField 或 TextFormField 组件的计数值,请尝试以下方法:
TextField(
decoration: InputDecoration(
hintText: "Email",
counterText: "",
),
maxLength: 40,
),
在这里,我已经在InputDecoration
属性中设置了counterText
属性,并将其设为空值。希望它能帮助到您。这是恰当的方法,它将防止文本字段下方出现额外空白,并避免增加设置空小部件所需的额外代码。
您可以在 TextField
中使用输入格式化程序。
以下是:
inputFormatters:[
LengthLimitingTextInputFormatter(1),
]
谢谢!
import 'package:flutter/services.dart';
- Joel BroströmInputDecoration.collapsed
方法,但是它并没有起作用。然而,user10481267的回答却有效。 - TokenyetmaxLength
参数。 - flomaster您可以使用InputDecoration来隐藏字母计数器。
TextFormField(
decoration: InputDecoration(
labelText: "username",
counterStyle: TextStyle(height: double.minPositive,),
counterText: ""
)
在textfield修饰中加入counterText: ''
可以隐藏计数器。这将仅显示一个空字符串。
只需将计数器设置为 Offstage() 即可解决问题。
TextField(
maxLines: 1,
decoration: InputDecoration(
counter: Offstage(),
),
),
你可以做:
TextField(
maxLength: 10,
buildCounter: (BuildContext context, { int currentLength, int maxLength, bool isFocused }) => null,
)
大多数答案似乎都可以运行。另一种方法是使用收缩的SizeBox来分配计数器。
TextField(decoration: InputDecoration(
hintText: "Email",
counter: SizedBox.shrink()
),
maxLength: 40,
),
TextField(
decoration: InputDecoration(
labelText: "Table Number",
counterText: ""
)
TextField(
maxLength: (some length),
buildCounter: (BuildContext context, {int currentLength, int maxLength, bool isFocused}) => null,
);
TextField( inputFormatters: [LengthLimitingTextInputFormatter(mMaxLength)] )
替换maxLength就可以解决问题,但需要在文件中添加额外的库。 - Joel BroströmcounterText
占用了空间高度并影响了用户界面。 - Farhana Naaz Ansari