This commit is contained in:
Hillel Coren 2020-10-24 21:36:37 +03:00
parent 61d8975b41
commit c6e6562805
2 changed files with 21 additions and 16 deletions

View File

@ -8,16 +8,16 @@ class TimePicker extends StatefulWidget {
Key key, Key key,
@required this.labelText, @required this.labelText,
@required this.onSelected, @required this.onSelected,
@required this.selectedDateTime,
@required this.selectedDate, @required this.selectedDate,
this.previousDate,
this.validator, this.validator,
this.autoValidate = false, this.autoValidate = false,
this.allowClearing = false, this.allowClearing = false,
}) : super(key: key); }) : super(key: key);
final String labelText; final String labelText;
final DateTime previousDate;
final DateTime selectedDate; final DateTime selectedDate;
final DateTime selectedDateTime;
final Function(DateTime) onSelected; final Function(DateTime) onSelected;
final Function validator; final Function validator;
final bool autoValidate; final bool autoValidate;
@ -39,9 +39,9 @@ class _TimePickerState extends State<TimePicker> {
@override @override
void didChangeDependencies() { void didChangeDependencies() {
if (widget.selectedDate != null) { if (widget.selectedDateTime != null) {
_textController.text = formatDate( _textController.text = formatDate(
widget.selectedDate.toIso8601String(), context, widget.selectedDateTime.toIso8601String(), context,
showDate: false, showTime: true); showDate: false, showTime: true);
} }
@ -51,7 +51,7 @@ class _TimePickerState extends State<TimePicker> {
void _onFoucsChanged() { void _onFoucsChanged() {
if (!_focusNode.hasFocus) { if (!_focusNode.hasFocus) {
_textController.text = formatDate( _textController.text = formatDate(
widget.selectedDate?.toIso8601String(), context, widget.selectedDateTime?.toIso8601String(), context,
showDate: false, showTime: true); showDate: false, showTime: true);
} }
} }
@ -65,11 +65,11 @@ class _TimePickerState extends State<TimePicker> {
} }
void _showTimePicker() async { void _showTimePicker() async {
final selectedDate = widget.selectedDate; final selectedDateTime = widget.selectedDateTime;
final now = DateTime.now(); final now = DateTime.now();
final hour = selectedDate?.hour ?? now.hour; final hour = selectedDateTime?.hour ?? now.hour;
final minute = selectedDate?.minute ?? now.minute; final minute = selectedDateTime?.minute ?? now.minute;
final TimeOfDay selectedTime = await showTimePicker( final TimeOfDay selectedTime = await showTimePicker(
context: context, context: context,
@ -78,10 +78,11 @@ class _TimePickerState extends State<TimePicker> {
); );
if (selectedTime != null) { if (selectedTime != null) {
var dateTime = convertTimeOfDayToDateTime(selectedTime, selectedDate); var dateTime =
convertTimeOfDayToDateTime(selectedTime, widget.selectedDate);
if (widget.previousDate != null && if (widget.selectedDate != null &&
dateTime.isBefore(widget.previousDate)) { dateTime.isBefore(widget.selectedDate)) {
dateTime = dateTime.add(Duration(days: 1)); dateTime = dateTime.add(Duration(days: 1));
} }
@ -103,7 +104,7 @@ class _TimePickerState extends State<TimePicker> {
controller: _textController, controller: _textController,
decoration: InputDecoration( decoration: InputDecoration(
labelText: widget.labelText, labelText: widget.labelText,
suffixIcon: widget.allowClearing && widget.selectedDate != null suffixIcon: widget.allowClearing && widget.selectedDateTime != null
? IconButton( ? IconButton(
icon: Icon(Icons.clear), icon: Icon(Icons.clear),
onPressed: () { onPressed: () {
@ -139,8 +140,8 @@ class _TimePickerState extends State<TimePicker> {
final dateTime = parseTime(value, context); final dateTime = parseTime(value, context);
print('## DATE TIME: $dateTime'); print('## DATE TIME: $dateTime');
if (dateTime != null) { if (dateTime != null) {
final date = widget.selectedDate ?? DateTime.now(); final date = widget.selectedDate;
final selectedDate = DateTime( var selectedDate = DateTime(
date.year, date.year,
date.month, date.month,
date.day, date.day,
@ -148,6 +149,9 @@ class _TimePickerState extends State<TimePicker> {
dateTime.minute, dateTime.minute,
dateTime.second, dateTime.second,
); );
if (selectedDate.isBefore(date)) {
selectedDate = selectedDate.add(Duration(days: 1));
}
widget.onSelected(selectedDate); widget.onSelected(selectedDate);
} }
} }

View File

@ -213,6 +213,7 @@ class TimeEditDetailsState extends State<TimeEditDetails> {
TimePicker( TimePicker(
labelText: localization.startTime, labelText: localization.startTime,
selectedDate: _startDate, selectedDate: _startDate,
selectedDateTime: _startDate,
onSelected: (timeOfDay) { onSelected: (timeOfDay) {
setState(() { setState(() {
_startDate = timeOfDay; _startDate = timeOfDay;
@ -225,8 +226,8 @@ class TimeEditDetailsState extends State<TimeEditDetails> {
TimePicker( TimePicker(
key: ValueKey(_duration), key: ValueKey(_duration),
labelText: localization.endTime, labelText: localization.endTime,
selectedDate: _endDate, selectedDate: _startDate,
previousDate: _startDate, selectedDateTime: _endDate,
allowClearing: true, allowClearing: true,
onSelected: (timeOfDay) { onSelected: (timeOfDay) {
setState(() { setState(() {