DateTimeFormatter<T extends DateTime?> constructor

DateTimeFormatter<T extends DateTime?>({
  1. DateTimeFormatterType type = DateTimeFormatterType.date,
  2. String? locale,
})

Create a date/time text input

Implementation

DateTimeFormatter({this.type = DateTimeFormatterType.date, this.locale}) {
  final d = DateFormat.yMd(locale);
  switch (type) {
    case DateTimeFormatterType.date:
      _dateFormat = d;
      break;
    case DateTimeFormatterType.dateShortTime:
      _dateFormat = d.add_Hm();
      break;
    case DateTimeFormatterType.dateFullTime:
      _dateFormat = d.add_Hms();
      break;
  }
  var s = _dateFormat.pattern!;
  //rewrite format use full digit
  s = s
      .replaceAll(RegExp(r'y+'), 'yyyy')
      .replaceAll(RegExp(r'M+'), 'MM')
      .replaceAll(RegExp(r'd+'), 'dd')
      .replaceAll(RegExp(r'H+'), 'HH')
      .replaceAll(RegExp(r'j+'), 'HH')
      .replaceAll(RegExp(r'm+'), 'mm')
      .replaceAll(RegExp(r's+'), 'ss');
  _dateFormat = DateFormat(s, locale);
  //rewrite masked text
  s = s
      .replaceAll(RegExp(r'y+'), '0000')
      .replaceAll(RegExp(r'M+'), '00')
      .replaceAll(RegExp(r'd+'), '00')
      .replaceAll(RegExp(r'H+'), '00')
      .replaceAll(RegExp(r'm+'), '00')
      .replaceAll(RegExp(r's+'), '00');
  _fm = _StringFormat(mask: s, keys: <String, RegExp>{'0': RegExp(r'[0-9]')});
}