Dart 颤振日期格式化程序工作不正常
*****编辑******* 所以现在我有了我所期望的格式,有人知道为什么一旦它出现在文本字段中,如果他们输入了一个额外的字符或者犯了一个错误,为什么他们不能退格和删除?下面是格式化的工作代码Dart 颤振日期格式化程序工作不正常,dart,flutter,Dart,Flutter,*****编辑******* 所以现在我有了我所期望的格式,有人知道为什么一旦它出现在文本字段中,如果他们输入了一个额外的字符或者犯了一个错误,为什么他们不能退格和删除?下面是格式化的工作代码 class _DateFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditing
class _DateFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue,
TextEditingValue newValue
) {
final int newTextLength = newValue.text.length;
int selectionIndex = newValue.selection.end;
int usedSubstringIndex = 0;
final StringBuffer newText = new StringBuffer();
/*if (newTextLength >= 1) {
newText.write('');
if (newValue.selection.end >= 1)
selectionIndex++;
}*/
if (newTextLength >= 2) {
newText.write(newValue.text.substring(0, usedSubstringIndex = 2) + '/');
if (newValue.selection.end >= 3)
selectionIndex += 2;
}
if (newTextLength >= 5) {
newText.write(newValue.text.substring(2, usedSubstringIndex = 4) + '/');
if (newValue.selection.end >= 5)
selectionIndex +=2;
}
if (newTextLength == 10) {
newText.write(newValue.text.substring(4, usedSubstringIndex = 8) + '');
if (newValue.selection.end >= 9)
selectionIndex +=4;
}
// Dump the rest.
if (newTextLength >= usedSubstringIndex)
newText.write(newValue.text.substring(usedSubstringIndex));
return new TextEditingValue(
text: newText.toString(),
selection: new TextSelection.collapsed(offset: selectionIndex),
);
}
}
感谢您的帮助。还有谁能想出一个原因,它在安卓系统中的工作方式与在iOS系统中的不同?您可以在DD/MM/YY分别使用一行三个文本字段
使用TextEditingController,您可以根据自己的喜好对输入的文本设置一些限制,例如:每天/每月2次键盘输入,一年4次键盘输入。此外,当达到当前文本字段的限制时,可以移动文本字段的焦点:
使用_dayFocus.unfocus,从键盘取消订阅日期文本字段。
使用FocusScope.ofcontext.requestFocus\u monthFocus,将焦点转移到月文本字段,依此类推。
例如:
代码:
我只是好奇你为什么不使用DatePicker,它更方便用户?我的客户希望能够只键入10位数字,他们觉得对于这些用户来说,picker太麻烦了。与其让它变得复杂,也许您可以在一行中创建3个文本字段,分别获取MM-DD-YY的值,并在获得结果时添加/。我认为这是一种更简单的方法,可能是一种可接受的解决方案,对用户来说也不太容易混淆。是否有一种简单的方法可以自动将焦点移动到每个框?请检查我添加的答案,它可能会很有用。谢谢您的示例,但这并不能避免用户在移动选择时必须进行交互,我想尝试将其保持在10个输入,我知道这听起来很极端,但客户想要他们想要的,我认为很容易采用我在JS中所做的并使其工作,但一直没有,也许有人会看到如何使格式化程序正常工作。谢谢你的帮助。我的意思是说8个输入。是的,我以为你的意思是8个输入,请检查答案中的更新部分。这非常有效!我只有一个问题,是否有任何方法可以检测退格并返回到最后一个文本字段。谢谢
import "package:flutter/material.dart";
import 'package:flutter/src/services/text_editing.dart';
class DateFormat extends StatefulWidget {
@override
_DateFormatState createState() => new _DateFormatState();
}
class _DateFormatState extends State<DateFormat> {
TextEditingController _dayController = new TextEditingController();
String day = "";
FocusNode _dayFocus = new FocusNode();
bool _dayFocused = true;
TextEditingController _monthController = new TextEditingController();
String month = "";
FocusNode _monthFocus = new FocusNode();
//bool _monthFocused=false;
TextEditingController _yearController = new TextEditingController();
String year = "";
FocusNode _yearFocus = new FocusNode();
//bool _yearFocused;
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
appBar: new AppBar(title: new Text("Date Format"),),
body: new Row(
children: <Widget>[ new Flexible (
child: new TextField(
keyboardType: TextInputType.datetime,
autofocus: _dayFocused,
focusNode: _dayFocus,
controller: _dayController,
onChanged: (value) {
if (value.length <= 2) {
//save
day = value;
}
else {
_dayFocus.unfocus();
FocusScope.of(context).requestFocus(_monthFocus);
// _monthFocused =true;
_dayController.value = new TextEditingValue(
text: day,
composing: new TextRange(
start: 0,
end: 2,
)
);
}
},
),),
new Text("/"),
new Flexible (
child: new TextField(
keyboardType: TextInputType.datetime,
focusNode: _monthFocus,
controller: _monthController,
onChanged: (value) {
if (value.length <= 2) {
//save
month = value;
}
else {
_monthFocus.unfocus();
FocusScope.of(context).requestFocus(_yearFocus);
_monthController.value = new TextEditingValue(
text: month,
composing: new TextRange(
start: 0,
end: 2,
)
);
}
},
),),
new Text("/"),
new Flexible (
child: new TextField(
keyboardType: TextInputType.datetime,
focusNode: _yearFocus,
controller: _yearController,
onChanged: (value) {
if (value.length <= 4) {
//save
year = value;
}
else {
_yearFocus.unfocus();
_yearController.value = new TextEditingValue(
text: year,
composing: new TextRange(
start: 0,
end: 4,
)
);
}
},
onSubmitted: (value) =>
_scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Text("Your date is $day/$month/$value"))),),),
],),);
}
}
import "package:flutter/material.dart";
import 'package:flutter/src/services/text_editing.dart';
class DateFormat extends StatefulWidget {
@override
_DateFormatState createState() => new _DateFormatState();
}
class _DateFormatState extends State<DateFormat> {
TextEditingController _dayController = new TextEditingController();
String day = "";
FocusNode _dayFocus = new FocusNode();
bool _dayFocused = true;
TextEditingController _monthController = new TextEditingController();
String month = "";
FocusNode _monthFocus = new FocusNode();
//bool _monthFocused=false;
TextEditingController _yearController = new TextEditingController();
String year = "";
FocusNode _yearFocus = new FocusNode();
//bool _yearFocused;
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
appBar: new AppBar(title: new Text("Date Format"),),
body: new Row(
children: <Widget>[ new Flexible (
child: new TextField(
keyboardType: TextInputType.datetime,
autofocus: _dayFocused,
focusNode: _dayFocus,
controller: _dayController,
onChanged: (value) {
if (value.length <= 2) {
//save
day = value;
}
else {
_dayFocus.unfocus();
_monthController.text = value[2];
_monthController.selection=new TextSelection.collapsed(offset: 1);
FocusScope.of(context).requestFocus(_monthFocus);
// _monthFocused =true;
_dayController.value = new TextEditingValue(
text: day,
composing: new TextRange(
start: 0,
end: 2,
)
);
}
},
),),
new Text("/"),
new Flexible (
child: new TextField(
keyboardType: TextInputType.datetime,
focusNode: _monthFocus,
controller: _monthController,
onChanged: (value) {
if (value.length <= 2) {
//save
//_monthController.text = value;
month = value;
}
else {
_yearController.text = value[2];
_yearController.selection=new TextSelection.collapsed(offset: 1);
_monthFocus.unfocus();
FocusScope.of(context).requestFocus(_yearFocus);
_monthController.value = new TextEditingValue(
text: month,
// selection: new TextSelection(baseOffset: 1, extentOffset: 1),
composing: new TextRange(
start: 0,
end: 2,
)
);
}
},
),),
new Text("/"),
new Flexible (
child: new TextField(
keyboardType: TextInputType.datetime,
focusNode: _yearFocus,
controller: _yearController,
onChanged: (value) {
if (value.length <= 4) {
//save
year = value;
}
else {
_yearFocus.unfocus();
_yearController.value = new TextEditingValue(
text: year,
composing: new TextRange(
start: 0,
end: 4,
)
);
}
},
onSubmitted: (value) =>
_scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Text("Your date is $day/$month/$value"))),),),
],),);
}
}