Skip to content

Commit

Permalink
Improved use of the local parameter to define DateTimePicker directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
hslbetto committed Nov 13, 2020
1 parent d788c97 commit 18716c7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.0 - 2020-11-12

* Improved use of the local parameter to define DateTimePicker directly.

## 1.0.2 - 2020-09-21

* Fix issue #4 - fixed the time display when period is 12h. Now it can differ correctly when time is AM or PM and show this information.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ In the `pubspec.yaml` of your flutter project, add the following dependency:
```yaml
dependencies:
...
date_time_picker: "^1.0.2"
date_time_picker: "^1.1.0"
```
In your library add the following import:
Expand Down
4 changes: 4 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class _MyHomePageState extends State<MyHomePage> {
dateLabelText: 'Date',
timeLabelText: "Hour",
//use24HourFormat: false,
//locale: Locale('pt', 'BR'),
selectableDayPredicate: (date) {
if (date.weekday == 6 || date.weekday == 7) {
return false;
Expand All @@ -123,6 +124,7 @@ class _MyHomePageState extends State<MyHomePage> {
//icon: Icon(Icons.event),
dateLabelText: 'Date Time',
use24HourFormat: false,
locale: Locale('en', 'US'),
onChanged: (val) => setState(() => _valueChanged2 = val),
validator: (val) {
setState(() => _valueToValidate2 = val);
Expand All @@ -139,6 +141,7 @@ class _MyHomePageState extends State<MyHomePage> {
lastDate: DateTime(2100),
icon: Icon(Icons.event),
dateLabelText: 'Date',
//locale: Locale('en', 'US'),
onChanged: (val) => setState(() => _valueChanged3 = val),
validator: (val) {
setState(() => _valueToValidate3 = val);
Expand All @@ -153,6 +156,7 @@ class _MyHomePageState extends State<MyHomePage> {
icon: Icon(Icons.access_time),
timeLabelText: "Time",
//use24HourFormat: false,
//locale: Locale('en', 'US'),
onChanged: (val) => setState(() => _valueChanged4 = val),
validator: (val) {
setState(() => _valueToValidate4 = val);
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies:
flutter:
sdk: flutter

date_time_picker: ^1.0.2
date_time_picker: ^1.1.0

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down
49 changes: 26 additions & 23 deletions lib/date_time_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class DateTimePicker extends FormField<String> {
controller != null ? controller.text : (initialValue ?? ''),
onSaved: onSaved,
validator: validator,
autovalidate: autovalidate,
//autovalidate: autovalidate,
enabled: enabled,
builder: (FormFieldState<String> field) {
final _DateTimePickerState state = field as _DateTimePickerState;
Expand Down Expand Up @@ -522,6 +522,7 @@ class _DateTimePickerState extends FormFieldState<String> {
String _sDate = '';
String _sTime = '';
String _sPeriod = '';
String _sLanguageCode;

@override
DateTimePicker get widget => super.widget as DateTimePicker;
Expand All @@ -540,24 +541,29 @@ class _DateTimePickerState extends FormFieldState<String> {
}

String lsValue = _effectiveController.text.trim();
String languageCode = widget.locale.languageCode;

if (widget.locale != null) {
_sLanguageCode = 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', languageCode).format(_dDate);
_sTime = DateFormat('HH:mm', languageCode).format(_dDate);

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

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

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

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

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

_dateLabelController.text =
DateFormat(lsMask, languageCode).format(_dDate);
DateFormat(lsMask, _sLanguageCode).format(_dDate);
}
} else {
List<String> llTime = lsValue.split(':');
Expand All @@ -591,7 +597,6 @@ 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 @@ -620,14 +625,14 @@ class _DateTimePickerState extends FormFieldState<String> {
_dDate = DateTime.tryParse(lsValue);

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

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

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

Expand All @@ -651,7 +656,7 @@ class _DateTimePickerState extends FormFieldState<String> {
}

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

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

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

Expand Down Expand Up @@ -818,9 +822,8 @@ class _DateTimePickerState extends FormFieldState<String> {
routeSettings: widget.routeSettings,
);

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

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

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

_dateLabelController.text = lsFormatedDate;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: date_time_picker
description: A Flutter widget to display a date time form field to show a date or clock dialog.
version: 1.0.2
version: 1.1.0
homepage: https://github.com/m3uzz/date_time_picker

environment:
Expand Down

0 comments on commit 18716c7

Please sign in to comment.