垂直居中
contentPadding: EdgeInsets.symmetric(vertical: 0),
border: OutlineInputBorder(borderSide: BorderSide.none),
设置maxLength,就会默认显示计数器,要不显示计数器:
decoration: InputDecoration(
counterText: ''
)
格式化手机号码,中间加入空格: 1xx 0000 1111
TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp('[0-9]+')),
TextInputFormatter.withFunction((oldValue, newValue) {
String text = newValue.text;
String positionStr = (text.substring(
0, newValue.selection.baseOffset))
.replaceAll(RegExp(r"\s+\b|\b\s"), "");
int length = positionStr.length;
int position = length;
if (length <= 3) {
position = length;
} else if (length <= 7) {
position = length + 1;
} else if (length <= 11) {
position = length + 2;
} else {
position = 13;
}
text = text.replaceAll(RegExp(r"\s+\b|\b\s"), "");
String string = "";
for (int i = 0; i < text.length; i++) {
if (i > 10) {
break;
}
if (i == 3 || i == 7) {
if (text[i] != " ") {
string = string + " ";
}
}
string = string + text[i];
}
text = string;
return TextEditingValue(
text: text,
selection: TextSelection.fromPosition(TextPosition(
offset: position,
affinity: TextAffinity.upstream)),
);
}),
LengthLimitingTextInputFormatter(13),
])
属性详情
TextField\
const TextField({
Key key,
this.controller,
this.focusNode,
this.decoration = const InputDecoration(),
TextInputType keyboardType,
this.textInputAction,
this.textCapitalization = TextCapitalization.none,
this.style,
this.strutStyle,
this.textAlign = TextAlign.start,
this.textDirection,
this.autofocus = false,
this.obscureText = false,
this.autocorrect = true,
this.maxLines = 1,
this.minLines,
this.expands = false,
this.maxLength,
this.maxLengthEnforced = true,
this.onChanged,
this.onEditingComplete,
this.onSubmitted,
this.inputFormatters,
this.enabled,
this.cursorWidth = 2.0,
this.cursorRadius,
this.cursorColor,
this.keyboardAppearance,
this.scrollPadding = const EdgeInsets.all(20.0),
this.dragStartBehavior = DragStartBehavior.start,
this.enableInteractiveSelection,
this.onTap,
this.buildCounter,
this.scrollPhysics,
})
InputDecoration
const InputDecoration({
this.icon,
this.labelText,
this.labelStyle,
this.helperText,
this.helperStyle,
this.hintText,
this.hintStyle,
this.hintMaxLines,
this.errorText,
this.errorStyle,
this.errorMaxLines,
this.hasFloatingPlaceholder = true,
this.isDense,
this.contentPadding,
this.prefixIcon,
this.prefix,
this.prefixText,
this.prefixStyle,
this.suffixIcon,
this.suffix,
this.suffixText,
this.suffixStyle,
this.counter,
this.counterText,
this.counterStyle,
this.filled,
this.fillColor,
this.errorBorder,
this.focusedBorder,
this.focusedErrorBorder,
this.disabledBorder,
this.enabledBorder,
this.border,
this.enabled = true,
this.semanticCounterText,
this.alignLabelWithHint,
})