'Setting Date format validations to a text field
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
Solution 1:[1]
Make DatePicker instead of keyboard to ensure a proper format.
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LMech |
| Solution 2 | nvoigt |
