Skip to content

Commit

Permalink
Fix issue #4 - fixed the time display when period is 12h. Now it can …
Browse files Browse the repository at this point in the history
…differ correctly when time is AM or PM and show this information.
  • Loading branch information
hslbetto committed Sep 21, 2020
1 parent bbe79ce commit a961141
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 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.

## 1.0.1 - 2020-08-18

* Fix issue #2 - just reference in README.md
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.1"
date_time_picker: "^1.0.2"
```
In your library add the following import:
Expand Down
5 changes: 4 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class _MyHomePageState extends State<MyHomePage> {
icon: Icon(Icons.event),
dateLabelText: 'Date',
timeLabelText: "Hour",
//use24HourFormat: false,
selectableDayPredicate: (date) {
if (date.weekday == 6 || date.weekday == 7) {
return false;
Expand All @@ -114,13 +115,14 @@ class _MyHomePageState extends State<MyHomePage> {
),
DateTimePicker(
type: DateTimePickerType.dateTime,
dateMask: 'd MMMM, yyyy - HH:mm',
dateMask: 'd MMMM, yyyy - hh:mm a',
controller: _controller2,
//initialValue: _initialValue,
firstDate: DateTime(2000),
lastDate: DateTime(2100),
//icon: Icon(Icons.event),
dateLabelText: 'Date Time',
use24HourFormat: false,
onChanged: (val) => setState(() => _valueChanged2 = val),
validator: (val) {
setState(() => _valueToValidate2 = val);
Expand Down Expand Up @@ -150,6 +152,7 @@ class _MyHomePageState extends State<MyHomePage> {
//initialValue: _initialValue,
icon: Icon(Icons.access_time),
timeLabelText: "Time",
//use24HourFormat: false,
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.1
date_time_picker: ^1.0.2

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down
60 changes: 57 additions & 3 deletions lib/date_time_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ class _DateTimePickerState extends FormFieldState<String> {
String _sValue;
String _sDate = '';
String _sTime = '';
String _sPeriod = '';

@override
DateTimePicker get widget => super.widget as DateTimePicker;
Expand Down Expand Up @@ -548,6 +549,10 @@ class _DateTimePickerState extends FormFieldState<String> {
_sDate = DateFormat('yyyy-MM-dd').format(_dDate);
_sTime = DateFormat('HH:mm').format(_dDate);

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

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

Expand All @@ -559,6 +564,10 @@ class _DateTimePickerState extends FormFieldState<String> {

if (widget.type == DateTimePickerType.dateTime && _sTime != '') {
lsMask = 'MMM d, yyyy - HH:mm';

if (!widget.use24HourFormat) {
lsMask = 'MMM d, yyyy - hh:mm a';
}
}

_dateLabelController.text = DateFormat(lsMask).format(_dDate);
Expand All @@ -568,7 +577,12 @@ class _DateTimePickerState extends FormFieldState<String> {
_tTime =
TimeOfDay(hour: int.parse(llTime[0]), minute: int.parse(llTime[1]));
_sTime = lsValue;
_timeLabelController.text = _sTime;

if (!widget.use24HourFormat) {
_sPeriod = _tTime.period.index == 0 ? ' AM' : ' PM';
}

_timeLabelController.text = _sTime + _sPeriod;
}
}
}
Expand Down Expand Up @@ -610,6 +624,10 @@ class _DateTimePickerState extends FormFieldState<String> {
if (lsOldTime != '') {
_tTime = TimeOfDay.fromDateTime(_dDate);
_sTime = DateFormat('HH:mm').format(_dDate);

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

Expand All @@ -625,6 +643,10 @@ class _DateTimePickerState extends FormFieldState<String> {

if (widget.type == DateTimePickerType.dateTime && _sTime != '') {
lsMask = 'MMM d, yyyy - HH:mm';

if (!widget.use24HourFormat) {
lsMask = 'MMM d, yyyy - hh:mm a';
}
}

_dateLabelController.text = DateFormat(lsMask).format(_dDate);
Expand All @@ -635,7 +657,7 @@ class _DateTimePickerState extends FormFieldState<String> {
_tTime = TimeOfDay(
hour: int.parse(llTime[0]), minute: int.parse(llTime[1]));
_sTime = lsValue;
_timeLabelController.text = _sTime;
_timeLabelController.text = _sTime + _sPeriod;
}
}
}
Expand Down Expand Up @@ -740,6 +762,15 @@ class _DateTimePickerState extends FormFieldState<String> {
if (ltTimePicked != null) {
String lsHour = ltTimePicked.hour.toString().padLeft(2, '0');
String lsMinute = ltTimePicked.minute.toString().padLeft(2, '0');

if (ltTimePicked.period.index == 0 && lsHour == '12') {
lsHour = '00';
}

if (!widget.use24HourFormat) {
_sPeriod = ltTimePicked.period.index == 0 ? ' AM' : ' PM';
}

_sTime = '$lsHour:$lsMinute';
_tTime = ltTimePicked;

Expand Down Expand Up @@ -795,7 +826,8 @@ class _DateTimePickerState extends FormFieldState<String> {
routeSettings: widget.routeSettings,
builder: (BuildContext context, Widget child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
data: MediaQuery.of(context)
.copyWith(alwaysUse24HourFormat: widget.use24HourFormat),
child: child,
);
},
Expand All @@ -804,8 +836,30 @@ class _DateTimePickerState extends FormFieldState<String> {
if (ltTimePicked != null) {
String lsHour = ltTimePicked.hour.toString().padLeft(2, '0');
String lsMinute = ltTimePicked.minute.toString().padLeft(2, '0');

if (ltTimePicked.period.index == 0 && lsHour == '12') {
lsHour = '00';
}

if (!widget.use24HourFormat) {
_sPeriod = ltTimePicked.period.index == 0 ? ' AM' : ' PM';
}

_sTime = '$lsHour:$lsMinute';
_tTime = ltTimePicked;
} else {
String lsHour = _tTime.hour.toString().padLeft(2, '0');
String lsMinute = _tTime.minute.toString().padLeft(2, '0');

if (_tTime.period.index == 0 && lsHour == '12') {
lsHour = '00';
}

if (!widget.use24HourFormat) {
_sPeriod = _tTime.period.index == 0 ? ' AM' : ' PM';
}

_sTime = '$lsHour:$lsMinute';
}

String lsOldValue = _sValue;
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.1
version: 1.0.2
homepage: https://github.com/m3uzz/date_time_picker

environment:
Expand Down

0 comments on commit a961141

Please sign in to comment.