Flutter 如何在颤振中输入编号的某些位置使用连字符创建文本字段
我正在制作一个UI,我必须在其中显示输入文本字段,如下所示:Flutter 如何在颤振中输入编号的某些位置使用连字符创建文本字段,flutter,dart,numbers,textfield,symbols,Flutter,Dart,Numbers,Textfield,Symbols,我正在制作一个UI,我必须在其中显示输入文本字段,如下所示: Container( width: 200, child: Center( child: TextFormField( keyboardType: TextInputType.number, inputFormatters: [ WhitelistingText
Container(
width: 200,
child: Center(
child: TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [
WhitelistingTextInputFormatter.digitsOnly,
new LengthLimitingTextInputFormatter(13),
new NumberFormatter()
],
),
),
),
//Custom InputFormatter
class NumberFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
var text = newValue.text;
if (newValue.selection.baseOffset == 0) {
return newValue;
}
var buffer = new StringBuffer();
for (int i = 0; i < text.length; i++) {
buffer.write(text[i]);
var nonZeroIndex = i + 1;
print(text.length);
if (nonZeroIndex <= 5) {
print("non");
print(nonZeroIndex);
if (nonZeroIndex % 5 == 0 && nonZeroIndex != text.length) {
buffer.write('-'); // Add double spaces.
}
} else {
if (nonZeroIndex % 12 == 0 && nonZeroIndex != text.length) {
buffer.write('-'); // Add double spaces.
}
}
}
var string = buffer.toString();
return newValue.copyWith(
text: string,
selection: new TextSelection.collapsed(offset: string.length));
}
}
我试图使它像这样,但无法使,因为我是新的扑动。在颤振中是否有任何类型的小部件可以满足此要求,或者您可以指导我如何制作此小部件。使用此软件包,您可以按以下方式进行操作。这将在用户键入数字时,在所需位置使用连字符自动格式化文本
class _MyWidgetState extends State<MyWidget> {
MaskedTextController tc = MaskedTextController(mask: '00000-0000000-0');
@override
Widget build(BuildContext context) {
return TextField(
controller: tc,
decoration: InputDecoration(
hintText: 'e.g. 61101-1234524-1',
),
);
}
}
使用此软件包,您可以按如下方式执行此操作。这将在用户键入数字时,在所需位置使用连字符自动格式化文本
class _MyWidgetState extends State<MyWidget> {
MaskedTextController tc = MaskedTextController(mask: '00000-0000000-0');
@override
Widget build(BuildContext context) {
return TextField(
controller: tc,
decoration: InputDecoration(
hintText: 'e.g. 61101-1234524-1',
),
);
}
}
你可以试试这个。如果要在一行中使用多个文本字段
Container(
width: 200,
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.blue)
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 50,
child: TextField(
keyboardType: TextInputType.number,
focusNode: f1,
onChanged: (String newVal) {
if (newVal.length == 5) {
f1.unfocus();
FocusScope.of(context).requestFocus(f2);
}
},
)),
Text(" - "),
Container(
width: 70,
child: TextField(
keyboardType: TextInputType.number,
focusNode: f2,
onChanged: (String newVal) {
if (newVal.length == 7) {
f2.unfocus();
FocusScope.of(context).requestFocus(f3);
}
if(newVal == ''){
f2.unfocus();
FocusScope.of(context).requestFocus(f1);
}
},
)),
Text(" - "),
Container(
width: 10,
child: TextField(
keyboardType: TextInputType.number,
focusNode: f3,
onChanged: (String newVal) {
if (newVal.length == 1) {
f3.unfocus();
}
if(newVal == ''){
f3.unfocus();
FocusScope.of(context).requestFocus(f2);
}
},
)),
],
),
),
输出:
但如果您想在单个文本字段中执行此操作,则应执行以下操作:
Container(
width: 200,
child: Center(
child: TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [
WhitelistingTextInputFormatter.digitsOnly,
new LengthLimitingTextInputFormatter(13),
new NumberFormatter()
],
),
),
),
//Custom InputFormatter
class NumberFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
var text = newValue.text;
if (newValue.selection.baseOffset == 0) {
return newValue;
}
var buffer = new StringBuffer();
for (int i = 0; i < text.length; i++) {
buffer.write(text[i]);
var nonZeroIndex = i + 1;
print(text.length);
if (nonZeroIndex <= 5) {
print("non");
print(nonZeroIndex);
if (nonZeroIndex % 5 == 0 && nonZeroIndex != text.length) {
buffer.write('-'); // Add double spaces.
}
} else {
if (nonZeroIndex % 12 == 0 && nonZeroIndex != text.length) {
buffer.write('-'); // Add double spaces.
}
}
}
var string = buffer.toString();
return newValue.copyWith(
text: string,
selection: new TextSelection.collapsed(offset: string.length));
}
}
输出:
你可以试试这个。如果要在一行中使用多个文本字段
Container(
width: 200,
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.blue)
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 50,
child: TextField(
keyboardType: TextInputType.number,
focusNode: f1,
onChanged: (String newVal) {
if (newVal.length == 5) {
f1.unfocus();
FocusScope.of(context).requestFocus(f2);
}
},
)),
Text(" - "),
Container(
width: 70,
child: TextField(
keyboardType: TextInputType.number,
focusNode: f2,
onChanged: (String newVal) {
if (newVal.length == 7) {
f2.unfocus();
FocusScope.of(context).requestFocus(f3);
}
if(newVal == ''){
f2.unfocus();
FocusScope.of(context).requestFocus(f1);
}
},
)),
Text(" - "),
Container(
width: 10,
child: TextField(
keyboardType: TextInputType.number,
focusNode: f3,
onChanged: (String newVal) {
if (newVal.length == 1) {
f3.unfocus();
}
if(newVal == ''){
f3.unfocus();
FocusScope.of(context).requestFocus(f2);
}
},
)),
],
),
),
输出:
但如果您想在单个文本字段中执行此操作,则应执行以下操作:
Container(
width: 200,
child: Center(
child: TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [
WhitelistingTextInputFormatter.digitsOnly,
new LengthLimitingTextInputFormatter(13),
new NumberFormatter()
],
),
),
),
//Custom InputFormatter
class NumberFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
var text = newValue.text;
if (newValue.selection.baseOffset == 0) {
return newValue;
}
var buffer = new StringBuffer();
for (int i = 0; i < text.length; i++) {
buffer.write(text[i]);
var nonZeroIndex = i + 1;
print(text.length);
if (nonZeroIndex <= 5) {
print("non");
print(nonZeroIndex);
if (nonZeroIndex % 5 == 0 && nonZeroIndex != text.length) {
buffer.write('-'); // Add double spaces.
}
} else {
if (nonZeroIndex % 12 == 0 && nonZeroIndex != text.length) {
buffer.write('-'); // Add double spaces.
}
}
}
var string = buffer.toString();
return newValue.copyWith(
text: string,
selection: new TextSelection.collapsed(offset: string.length));
}
}
输出:
例如:谢谢你的回答。我也赞成你的回答。因为它对新的颤振开发者帮助很大。当我输入一个值12345-6789000-0并删除一个值时,我将光标移动到3并删除3,然后我的光标自动移动到最后一位0,为什么?谢谢你的回答。我也赞成你的回答。当我输入一个值12345-6789000-0并删除一个值时,我将光标移动到3并删除3,然后我的光标自动移动到最后一位0,为什么?