Created
May 12, 2020 17:46
-
-
Save yongjhih/e686bf53c844eea54747821f170ec596 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref. https://medium.com/@gauthamns/date-time-input-fields-in-flutter-ae74ffbf4ef2 | |
class DateTimePicker extends StatefulWidget { | |
const DateTimePicker({ | |
Key key, | |
this.labelText, | |
@required | |
this.selectedDate, | |
@required | |
this.selectedTime, | |
this.onSelectDate, | |
this.onSelectTime, | |
}) : super(key: key); | |
final String labelText; | |
final DateTime selectedDate; | |
final TimeOfDay selectedTime; | |
final ValueChanged<DateTime> onSelectDate; | |
final ValueChanged<TimeOfDay> onSelectTime; | |
@override | |
State<DateTimePicker> createState() => _DateTimePickerState(); | |
} | |
class _DateTimePickerState extends State<DateTimePicker> { | |
DateTime _selectedDate; | |
@override | |
void initState() { | |
super.initState(); | |
initialize(); | |
} | |
void initialize() { | |
_selectedDate = widget.selectedDate; | |
_selectedDate = _selectedDate.copyWith( | |
hour: widget.selectedTime.hour, | |
minute: widget.selectedTime.minute, | |
); | |
} | |
@override | |
void didUpdateWidget(DateTimePicker oldWidget) { | |
if (oldWidget.selectedDate != widget.selectedDate | |
|| oldWidget.selectedTime != widget.selectedTime) { | |
initialize(); | |
} | |
super.didUpdateWidget(oldWidget); | |
} | |
Future<void> _selectDate(BuildContext context) async { | |
final DateTime picked = await showDatePicker( | |
context: context, | |
initialDate: _selectedDate, | |
firstDate: DateTime(1970, 8), | |
lastDate: DateTime.now() | |
); | |
if (picked != null && picked != _selectedDate) { | |
setState(() { | |
_selectedDate = picked; | |
}); | |
widget.onSelectDate?.call(picked); | |
} | |
} | |
Future<void> _selectTime(BuildContext context) async { | |
final TimeOfDay picked = await showTimePicker( | |
context: context, | |
initialTime: _selectedDate.toTimeOfDay() | |
); | |
if (picked != null && picked != _selectedDate.toTimeOfDay()) { | |
setState(() { | |
_selectedDate = _selectedDate.copyWith( | |
hour: picked.hour, | |
minute: picked.minute, | |
); | |
}); | |
widget.onSelectTime?.call(picked); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
final TextStyle valueStyle = Theme.of(context).textTheme.body1; | |
return Row( | |
crossAxisAlignment: CrossAxisAlignment.end, | |
children: <Widget>[ | |
Expanded( | |
flex: 4, | |
child: _InputDropdown( | |
labelText: widget.labelText, | |
valueText: intl.DateFormat.yMMMd().format(_selectedDate), | |
valueStyle: valueStyle, | |
onPressed: () { | |
_selectDate(context); | |
}, | |
), | |
), | |
const SizedBox(width: 12.0), | |
Expanded( | |
flex: 3, | |
child: _InputDropdown( | |
valueText: _selectedDate.toTimeOfDay().format(context), | |
valueStyle: valueStyle, | |
onPressed: () { | |
_selectTime(context); | |
}, | |
), | |
), | |
], | |
); | |
} | |
} | |
class _InputDropdown extends StatelessWidget { | |
const _InputDropdown( | |
{Key key, | |
this.child, | |
this.labelText, | |
this.valueText, | |
this.valueStyle, | |
this.onPressed}) | |
: super(key: key); | |
final String labelText; | |
final String valueText; | |
final TextStyle valueStyle; | |
final VoidCallback onPressed; | |
final Widget child; | |
@override | |
Widget build(BuildContext context) { | |
return InkWell( | |
onTap: onPressed, | |
child: InputDecorator( | |
decoration: InputDecoration( | |
labelText: labelText, | |
), | |
baseStyle: valueStyle, | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
Text(valueText, style: valueStyle), | |
Icon(Icons.arrow_drop_down, | |
color: Theme.of(context).brightness == Brightness.light | |
? Colors.grey.shade700 | |
: Colors.white70), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment