1、添加InputFormatter类继承TextInputFormatter,更加输入根据正则表达式进行匹配,匹配成功返回新的内容,不成功就不让输入
class InputFormatter extends TextInputFormatter {
final String regExp;
InputFormatter(this.regExp);
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
if (newValue.text.length > 0) {
if (RegExp(regExp).firstMatch(newValue.text) != null) {
return newValue;
}
return oldValue;
}
return newValue;
}
}
2、在TextField 中的inputFormatters参数中试用它
Widget _pointsWidget() {
String regExp = r"^((\-|\+)?(\d+)?)$";
return Expanded(
flex: 3,
child: Container(
padding: EdgeInsets.only(right: 30),
child: TextField(
style: TextStyle(fontSize: 15.5),
controller: _pointsCtrl,
inputFormatters: [InputFormatter(regExp)],
decoration: outLineDecoration(5, isDense: true),
keyboardType: TextInputType.number,
onChanged: (v) {
_countPoints();
},
),
),
);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)