flutter TextField输入长度限制

- 日理万妓 2021-06-22 15:36 1277阅读 1赞

在Flutter中一个中文是一个长度,这里我们将长度限制在20字,可以按照下面这种方式做:

  1. TextField(
  2. style: TextStyle(fontSize: ScreenUtil().setWidth(16), color: Colors.black),
  3. controller: _cpyCode,//控制器
  4. decoration: InputDecoration(
  5. hintText: '请输入标题',
  6. hintStyle: TextStyle( fontWeight: FontWeight.w600, fontSize: ScreenUtil().setWidth(40), color: Colors.grey[400]),
  7. border: InputBorder.none,
  8. ),
  9. inputFormatters: <TextInputFormatter>[
  10. LengthLimitingTextInputFormatter(20)//限制长度
  11. ],
  12. onChanged: _listenCpyCode,
  13. )),

其中LengthLimitingTextInputFormatter(20)这个就是限制TextField长度,当长度大于20的时候就不会再输入了。

觉得有用的话,可以点个赞。

发表评论

表情:
评论列表 (有 0 条评论,1277人围观)

还没有评论,来说两句吧...

相关阅读