flutter 设置文本字段的日期格式验证

nc1teljy  于 2023-03-09  发布在  Flutter
关注(0)|答案(3)|浏览(161)

我已经创建了两个名为开始日期和结束日期的文本字段,应该只接受格式为yyyy/mm/dd的日期。现在用户可以输入任何格式。如何设置验证用户输入的日期在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)),
                      ),
                    ),
                  ),
                ],
              ),

这里的TxtField是我使用TextFormField创建的自定义文本字段

hyrbngr7

hyrbngr71#

使用DatePicker代替键盘,以确保格式正确。

kyks70gy

kyks70gy2#

您真的应该使用DatePicker:What is the correct way to add date picker in flutter app?
如果你坚持用非标准的方式来做,你可以使用FilteringTextInputFormatter:Flutter - Regex in TextFormField

d4so4syb

d4so4syb3#

您可以将其用作inputFormatters,将TextInputType.number用作keyboardType

class DateTextFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    if (newValue.text.length > oldValue.text.length &&
        newValue.text.isNotEmpty &&
        oldValue.text.isNotEmpty) {
      if (RegExp('[^0-9/]').hasMatch(newValue.text)) return oldValue;
      if (newValue.text.length > 10) return oldValue;
      if (newValue.text.length == 2 || newValue.text.length == 5) {
        return TextEditingValue(
          text: '${newValue.text}/',
          selection: TextSelection.collapsed(
            offset: newValue.selection.end + 1,
          ),
        );
      } else if (newValue.text.length == 3 && newValue.text[2] != '/') {
        return TextEditingValue(
          text:
              '${newValue.text.substring(0, 2)}/${newValue.text.substring(2)}',
          selection: TextSelection.collapsed(
            offset: newValue.selection.end + 1,
          ),
        );
      } else if (newValue.text.length == 6 && newValue.text[5] != '/') {
        return TextEditingValue(
          text:
              '${newValue.text.substring(0, 5)}/${newValue.text.substring(5)}',
          selection: TextSelection.collapsed(
            offset: newValue.selection.end + 1,
          ),
        );
      }
    } else if (newValue.text.length == 1 &&
        oldValue.text.isEmpty &&
        RegExp('[^0-9]').hasMatch(newValue.text)) {
      return oldValue;
    }
    return newValue;
  }
}

相关问题