新建
import 'package:flutter/services.dart';
class InputFormatter extends TextInputFormatter {
final String regExp;
InputFormatter(this.regExp);
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
if (newValue.text.isNotEmpty) {
if (RegExp(regExp).firstMatch(newValue.text) != null) {
return newValue;
}
return oldValue;
}
return newValue;
}
}
child中
TextFormField(
inputFormatters: [
InputFormatter('[a-zA-Z]|[\u4e00-\u9fa5]'),//限制只能输入中文和英文
])
之前的WhitelistingTextInputFormatter(只能输入)和BlacklistingTextInputFormatter(不能输入)这两个已经找不到了