我试图添加一个日期掩码的textField,因为我不喜欢的日期选择器,因为出生日期,例如,它不是那么灵活。之后,从字符串转换到日期时间,我相信我可以继续该项目,提前感谢.
static final TextEditingController _birthDate = new TextEditingController();
new TextFormField(
controller: _birthDate,
maxLength: 10,
keyboardType: TextInputType.datetime,
validator: _validateDate
), String _validateDate(String value) {
if(value.isEmpty)
return null;
if(value.length != 10)
return 'Enter date in DD / MM / YYYY format';
return null;
}
6条答案
按热度按时间0s0u357o1#
我修改了一些东西,并设法得到预期的结果。
我创建了这个类来定义变量
最后我在文本域中添加了一个输入格式
现在没事了,谢谢
quhf5bfb2#
https://pub.dartlang.org/packages/masked_text
masked_text
一个用于屏蔽文本的软件包,所以如果你想要一个电话屏蔽,或邮政编码或任何类型的屏蔽,只要使用它:D
入门
这很简单,它是一个小部件,因为所有其他的。
'x'是你的文本将具有的正常字符。
这个示例最后重现了这样的内容:
11/02/1995
.wa7juj8i3#
我的解决方案:
e7arh2l64#
此解决方案检查日期何时超出范围(例如没有一个月像13)。效率很低,但很有效。
polkgigr5#
你可以使用flutter包mask_text_input_formatter(有超过850个赞)
使用方法:
qc6wkl3g6#
现在可以使用TextField的布尔属性“obscureText”来屏蔽输入。