将日期格式验证设置为文本字段
我创建了两个名为starrt日期和结束日期的文本字段,它们应以格式yyyy/mm/dd的格式接受日期。现在,用户可以以任何格式输入它。如何为用户设置验证以输入yyyy/yyyy/ mm/dd。
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: Padding(
padding: const EdgeInsets.all(3.0),
child: TxtField(
fillColor: Cultured,
labelText: 'Start Date',
hintText: '2022-01-10',
onChanged: (value) => context
.read<CareRequirementsScreenBloc>()
.add(StartDateChanged(value)),
),
),
),
Flexible(
child: Padding(
padding: const EdgeInsets.all(3.0),
child: TxtField(
fillColor: Cultured,
labelText: 'End Date',
hintText: '2022-01-10',
onChanged: (value) => context
.read<CareRequirementsScreenBloc>()
.add(EndDateChanged(value)),
),
),
),
],
),
这里是我使用textformfield创建的自定义文本字段
I have created two text fields named starrt date and end date which should accept dates in the format yyyy/mm/dd only.Right now user can input it in any format.How can set validations for the user to input the date in yyyy/mm/dd.
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: Padding(
padding: const EdgeInsets.all(3.0),
child: TxtField(
fillColor: Cultured,
labelText: 'Start Date',
hintText: '2022-01-10',
onChanged: (value) => context
.read<CareRequirementsScreenBloc>()
.add(StartDateChanged(value)),
),
),
),
Flexible(
child: Padding(
padding: const EdgeInsets.all(3.0),
child: TxtField(
fillColor: Cultured,
labelText: 'End Date',
hintText: '2022-01-10',
onChanged: (value) => context
.read<CareRequirementsScreenBloc>()
.add(EndDateChanged(value)),
),
),
),
],
),
Here TxtField is a custom text field I have created using TextFormField
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
制作 /a>而不是键盘以确保正确的格式。
Make
DatePicker
instead of keyboard to ensure a proper format.您应该真正使用datepicker:什么在Flutter应用中添加日期选择器的正确方法是吗?
如果您坚持使用非标准方式,则可以使用FilteringTextInputFormatter: flutter- textformfield中的正则
You should really use a DatePicker: What is the correct way to add date picker in flutter app?
If you insist on doing it the non-standard way, you can use a FilteringTextInputFormatter: Flutter - Regex in TextFormField
您可以将其用作
inputFormatters
和textInputType.number
作为keyboardType
。You can use this as
inputFormatters
andTextInputType.number
askeyboardType
.您可以使用InputDatePickerFormField,该输入基于用户的语言环境验证输入:
https://api.flutter.dev/flutter.dev/flutter.dev/flutter.dev/flutter/flutter/material/material/material/inputdatepickerfield-class-class-class-class-class-class-class-class-class-class-class-class-class。 html
You can use InputDatePickerFormField which validates the input based on the user's locale:
https://api.flutter.dev/flutter/material/InputDatePickerFormField-class.html