selectDate static method
void
selectDate({
- required BuildContext context,
- DateTime? firstDate,
- DateTime? initialDate,
- DateTime? lastDate,
- required TextEditingController controller,
- void onDateSelected(
- DateTime? date
Implementation
static void selectDate({
required BuildContext context,
DateTime? firstDate,
DateTime? initialDate,
DateTime? lastDate,
required TextEditingController controller,
void Function(DateTime? date)? onDateSelected,
}) async {
try {
// Parse the current date from the controller
final currentDate = Utils().parseDDMMYYYYToDate(controller.text);
// Show date picker
final pickedDate = await showDatePicker(
context: context,
initialDate: initialDate ?? DateTime.now(),
firstDate: firstDate ?? DateTime(2000, 1),
lastDate: lastDate ?? DateTime(2500, 12),
builder: (context, child) {
return Theme(
data: Theme.of(context).copyWith(
textTheme: const TextTheme(
titleLarge: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD, color: Colors.black),
bodyLarge: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD, color: Colors.black),
bodySmall: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD, color: Colors.black),
labelLarge: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD, color: Colors.black),
bodyMedium: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD, color: Colors.black),
displayLarge: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD, color: Colors.black),
displayMedium: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD),
displaySmall: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD),
headlineLarge: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD),
headlineMedium: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD),
headlineSmall: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD),
labelMedium: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD),
labelSmall: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD),
titleMedium: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD, color: Colors.black),
titleSmall: TextStyle(fontFamily: FONT_STYLE_QUICK_BOLD, color: Colors.black),
),
colorScheme: const ColorScheme.light(
primary: Colors.deepPurple,
onPrimary: Colors.white,
onSurface: Colors.black,
),
dialogBackgroundColor: Colors.white,
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
foregroundColor: Colors.deepPurple,
),
),
),
child: child!,
);
},
);
// Handle the selected date
if (pickedDate != null) {
initialDate = pickedDate;
controller.text = Utils().getFormatedDateDDMMYYYYT(pickedDate.toString());
onDateSelected?.call(pickedDate);
}
} catch (e) {
// Fallback to current date if parsing fails
controller.text = Utils().getFormatedDateDDMMYYYYT(DateTime.now().toString());
}
}