Skip to content

Instantly share code, notes, and snippets.

@MeherajUlMahmmud
Created November 26, 2021 06:42
Show Gist options
  • Save MeherajUlMahmmud/1d2598a9c4ff4033347d7af56bebccc4 to your computer and use it in GitHub Desktop.
Save MeherajUlMahmmud/1d2598a9c4ff4033347d7af56bebccc4 to your computer and use it in GitHub Desktop.
Future pickDateTime(BuildContext context) async {
final date = await pickDate(context);
if (date == null) return;
final time = await pickTime(context);
if (time == null) return;
setState(() {
dateTime = DateTime(
date.year,
date.month,
date.day,
time.hour,
time.minute,
);
String formattedDate =
DateFormat('yyyy-MM-dd - kk:mm:a').format(dateTime);
List<String> dateTimeList = formattedDate.split(' - ');
_date = dateTimeList[0];
_time = dateTimeList[1];
List<String> timeList = _endingTime.split(':');
String hour = timeList[0];
String minute = timeList[1];
String amPm = timeList[2];
if (int.parse(hour) > 12) {
hour = (int.parse(hour) - 12).toString();
}
_time = hour + ':' + minute + ' ' + amPm;
_timeController.text = "On " + _date + " At " + _time;
});
}
Future<DateTime> pickDate(BuildContext context) async {
final initialDate = DateTime.now();
final newDate = await showDatePicker(
context: context,
initialDate: dateTime ?? initialDate,
firstDate: DateTime(DateTime.now().year - 1),
lastDate: DateTime(DateTime.now().year + 1),
);
if (newDate == null) return null;
return newDate;
}
Future<TimeOfDay> pickTime(BuildContext context) async {
final initialTime = TimeOfDay(hour: 9, minute: 0);
final newTime = await showTimePicker(
context: context,
initialTime: dateTime != null
? TimeOfDay(hour: dateTime.hour, minute: dateTime.minute)
: initialTime,
);
if (newTime == null) return null;
return newTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment