flutter TextField输入长度限制
在Flutter中一个中文是一个长度,这里我们将长度限制在20字,可以按照下面这种方式做:
TextField(
style: TextStyle(fontSize: ScreenUtil().setWidth(16), color: Colors.black),
controller: _cpyCode,//控制器
decoration: InputDecoration(
hintText: '请输入标题',
hintStyle: TextStyle( fontWeight: FontWeight.w600, fontSize: ScreenUtil().setWidth(40), color: Colors.grey[400]),
border: InputBorder.none,
),
inputFormatters: <TextInputFormatter>[
LengthLimitingTextInputFormatter(20)//限制长度
],
onChanged: _listenCpyCode,
)),
其中LengthLimitingTextInputFormatter(20)
这个就是限制TextField长度,当长度大于20的时候就不会再输入了。
觉得有用的话,可以点个赞。
还没有评论,来说两句吧...