1.引入头文件
import 'package:flutter/services.dart';
2.
限制TextField 的输入长度需要inputFormatters 的属性及属性值:LengthLimitingTextInputFormatter(6)
代码如下
TextField(
style: TextStyle(fontSize: ScreenUtil().setWidth(16), color: Colors.black),
controller: _controller,//控制器
decoration: InputDecoration(
hintText: '请输入标题',
hintStyle: TextStyle( fontWeight: FontWeight.w500, fontSize: ScreenUtil().setWidth(20), color: Colors.grey[400]),
border: InputBorder.none,
),
inputFormatters: <TextInputFormatter>[
LengthLimitingTextInputFormatter(6)//限制长度
],
onChanged: (v){
// textField输入内容的回调
},
)),