我只想在文本表单字段中获取输入文本计数器和最大长度
TextFormField(
controller: newusrname, //u can ignore this controller
maxLength: 30, //this is the limit i have set
decoration: InputDecoration(
prefixIcon: Icon(Icons.account_box),
labelText: "Username",
labelStyle: TextStyle(
fontSize: 20.0,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0)),
),
),
答案 0 :(得分:1)
试试这个
TextEditingController _controller = new TextEditingController();
String text = ""; // empty string to carry what was there before it
int maxLength = 30;
TextFormField(
controller: _controller,
decoration: InputDecoration(
prefixIcon: Icon(Icons.account_box),
labelText: "Username",
suffix: Text("${_controller.text.length}/$maxLength"),
labelStyle: TextStyle(
fontSize: 20.0,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0)),
),
onChanged: (String newVal) {
setState(() {
if (newVal.length <= maxLength) {
text = newVal;
} else {
_controller.value = new TextEditingValue(
text: text,
selection: new TextSelection(
baseOffset: maxLength,
extentOffset: maxLength,
affinity: TextAffinity.downstream,
isDirectional: false),
composing: new TextRange(start: 0, end: maxLength));
_controller.text = text;
}
});
},
),
答案 1 :(得分:0)
不,您不能使用默认的 TextFormField
更改文本长度指示器的位置。但是,例如,您可以构建自己的文本长度指示器小部件并将其放置在文本字段旁边。尽管如此,这仍然不允许你把它放在里面。