Home > Software design >  Setting Date format validations to a text field
Setting Date format validations to a text field

Time:05-03

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

CodePudding user response:

Make DatePicker instead of keyboard to ensure a proper format.

CodePudding user response:

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

  • Related