问题描述
有没有办法将字符计数器定位在 Flutter 中的文本字段内?我发现其他人的问题询问了自定义计数器并设法将其放置到位,但它仍然在场地下方和场地之外。我很想把它放在后缀所在的地方,但我敢打赌这是不可能的。有没有其他方法可以让计数器出现在字段内?
提前致谢!
解决方法
获取文本的长度,然后将后缀文本更改为等于该长度。
像这样
class MyTextField extends StatefulWidget {
@override
_MyTextFieldState createState() => _MyTextFieldState();
}
class _MyTextFieldState extends State<MyTextField> {
var maxLength = 10;
var textLength = 0;
@override
Widget build(BuildContext context) {
return TextFormField(
decoration: InputDecoration(
contentPadding: EdgeInsets.all(15),suffixText: '${textLength.toString()}/${maxLength.toString()}',counterText: "",),cursorRadius: Radius.circular(10),keyboardType: TextInputType.text,autofocus: true,maxLength: maxLength,onChanged: (value) {
setState(() {
textLength = value.length;
});
},);
}
}