Skip to content

Commit

Permalink
Merge pull request #6 from hovadur/master
Browse files Browse the repository at this point in the history
language code in DateFormat
  • Loading branch information
hslbetto authored Nov 13, 2020
2 parents a961141 + 4022ae0 commit d788c97
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions lib/date_time_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -540,25 +540,24 @@ class _DateTimePickerState extends FormFieldState<String> {
}

String lsValue = _effectiveController.text.trim();

String languageCode = widget.locale.languageCode;
if (lsValue != null && lsValue != '' && lsValue != 'null') {
if (widget.type != DateTimePickerType.time) {
_dDate = DateTime.tryParse(lsValue);
_tTime = TimeOfDay.fromDateTime(_dDate);

_sDate = DateFormat('yyyy-MM-dd').format(_dDate);
_sTime = DateFormat('HH:mm').format(_dDate);
_sDate = DateFormat('yyyy-MM-dd', languageCode).format(_dDate);
_sTime = DateFormat('HH:mm', languageCode).format(_dDate);

if (!widget.use24HourFormat) {
_sTime = DateFormat('hh:mm a').format(_dDate);
_sTime = DateFormat('hh:mm a', languageCode).format(_dDate);
}

_timeLabelController.text = _sTime;
_dateLabelController.text = _sDate;

if (widget.dateMask != null && widget.dateMask != '') {
_dateLabelController.text =
DateFormat(widget.dateMask).format(_dDate);
DateFormat(widget.dateMask, languageCode).format(_dDate);
} else {
String lsMask = 'MMM d, yyyy';

Expand All @@ -570,7 +569,8 @@ class _DateTimePickerState extends FormFieldState<String> {
}
}

_dateLabelController.text = DateFormat(lsMask).format(_dDate);
_dateLabelController.text =
DateFormat(lsMask, languageCode).format(_dDate);
}
} else {
List<String> llTime = lsValue.split(':');
Expand All @@ -591,6 +591,7 @@ class _DateTimePickerState extends FormFieldState<String> {
void didUpdateWidget(DateTimePicker oldWidget) {
super.didUpdateWidget(oldWidget);

String languageCode = widget.locale.languageCode;
if (widget.controller != oldWidget.controller) {
oldWidget.controller?.removeListener(_handleControllerChanged);
widget.controller?.addListener(_handleControllerChanged);
Expand Down Expand Up @@ -619,14 +620,14 @@ class _DateTimePickerState extends FormFieldState<String> {
_dDate = DateTime.tryParse(lsValue);

if (_dDate != null) {
_sDate = DateFormat('yyyy-MM-dd').format(_dDate);
_sDate = DateFormat('yyyy-MM-dd', languageCode).format(_dDate);

if (lsOldTime != '') {
_tTime = TimeOfDay.fromDateTime(_dDate);
_sTime = DateFormat('HH:mm').format(_dDate);
_sTime = DateFormat('HH:mm', languageCode).format(_dDate);

if (!widget.use24HourFormat) {
_sTime = DateFormat('hh:mm a').format(_dDate);
_sTime = DateFormat('hh:mm a', languageCode).format(_dDate);
}
}
}
Expand All @@ -637,7 +638,7 @@ class _DateTimePickerState extends FormFieldState<String> {
if (_dDate != null) {
if (widget.dateMask != null && widget.dateMask != '') {
_dateLabelController.text =
DateFormat(widget.dateMask).format(_dDate);
DateFormat(widget.dateMask, languageCode).format(_dDate);
} else {
String lsMask = 'MMM d, yyyy';

Expand All @@ -649,7 +650,8 @@ class _DateTimePickerState extends FormFieldState<String> {
}
}

_dateLabelController.text = DateFormat(lsMask).format(_dDate);
_dateLabelController.text =
DateFormat(lsMask, languageCode).format(_dDate);
}
}
} else {
Expand Down Expand Up @@ -715,19 +717,20 @@ class _DateTimePickerState extends FormFieldState<String> {
routeSettings: widget.routeSettings,
);

String languageCode = widget.locale.languageCode;
if (ldDatePicked != null) {
_sDate = DateFormat('yyyy-MM-dd').format(ldDatePicked);
_sDate = DateFormat('yyyy-MM-dd', languageCode).format(ldDatePicked);
_dDate = ldDatePicked;
String lsOldValue = _sValue;
_sValue = _sDate;
String lsFormatedDate;

if (widget.dateMask != null && widget.dateMask != '') {
lsFormatedDate =
DateFormat(widget.dateMask).format(DateTime.tryParse(_sDate));
lsFormatedDate = DateFormat(widget.dateMask, languageCode)
.format(DateTime.tryParse(_sDate));
} else {
lsFormatedDate =
DateFormat('MMM dd, yyyy').format(DateTime.tryParse(_sDate));
lsFormatedDate = DateFormat('MMM dd, yyyy', languageCode)
.format(DateTime.tryParse(_sDate));
}

if (widget.type == DateTimePickerType.dateTimeSeparate && _sTime != '') {
Expand Down Expand Up @@ -815,8 +818,9 @@ class _DateTimePickerState extends FormFieldState<String> {
routeSettings: widget.routeSettings,
);

String languageCode = widget.locale.languageCode;
if (ldDatePicked != null) {
_sDate = DateFormat('yyyy-MM-dd').format(ldDatePicked);
_sDate = DateFormat('yyyy-MM-dd', languageCode).format(ldDatePicked);
_dDate = ldDatePicked;

TimeOfDay ltTimePicked = await showTimePicker(
Expand Down Expand Up @@ -867,11 +871,12 @@ class _DateTimePickerState extends FormFieldState<String> {
_sValue = _sValue.trim();

if (widget.dateMask != null && widget.dateMask != '') {
lsFormatedDate =
DateFormat(widget.dateMask).format(DateTime.tryParse(_sValue));
lsFormatedDate = DateFormat(widget.dateMask, languageCode)
.format(DateTime.tryParse(_sValue));
} else {
String lsMask = _sTime != '' ? 'MMM dd, yyyy - HH:mm' : 'MMM dd, yyyy';
lsFormatedDate = DateFormat(lsMask).format(DateTime.tryParse(_sValue));
lsFormatedDate =
DateFormat(lsMask, languageCode).format(DateTime.tryParse(_sValue));
}

_dateLabelController.text = lsFormatedDate;
Expand Down

0 comments on commit d788c97

Please sign in to comment.