selectTime method
Future<void>
selectTime({
- required BuildContext context,
- required TextEditingController controller,
- required TimeOfDay initialTime,
Implementation
Future<void> selectTime(
{required BuildContext context,
required TextEditingController controller,
required TimeOfDay initialTime}) async {
TimeOfDay selectedTime = initialTime;
dynamic picked;
final TargetPlatform _platForm = Theme.of(context).platform;
if (_platForm == TargetPlatform.iOS) {
final DateTime minimumDateTime = DateTime(currentYear, currentMonth,
currentDay, TimeOfDay.now().hour, TimeOfDay.now().minute);
await showModalBottomSheet(
context: context,
builder: (BuildContext builder) {
return SizedBox(
height: MediaQuery.of(context).copyWith().size.height / 3,
child: CupertinoDatePicker(
mode: CupertinoDatePickerMode.time,
initialDateTime: minimumDateTime,
minimumDate: minimumDateTime,
onDateTimeChanged: (DateTime changedTimer) {
picked = TimeOfDay.fromDateTime(changedTimer);
},
),
);
});
} else {
picked = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
builder: (BuildContext? context, Widget? child) {
return MediaQuery(
data: MediaQuery.of(context!)
.copyWith(alwaysUse24HourFormat: false),
child: Theme(
data: Theme.of(context).copyWith(
primaryColor: healthcloudAccentColor,
),
child: child!,
),
);
});
}
if (picked != null && picked != selectedTime) {
selectedTime = picked as TimeOfDay;
}
String _convertTimeToString(TimeOfDay time) {
//format time to format as "6.05 PM"
final DateTime now = DateTime.now();
final DateTime formattedDateTime =
DateTime(now.year, now.month, now.day, time.hour, time.minute);
return DateFormat.jm().format(formattedDateTime);
}
controller.text = _convertTimeToString(selectedTime);
}