In Flutter showDatePicker(material), when the user switches to input mode by clicking on the switchToInputEntryModeIcon, the default date format is "mm/dd/yyyy". However, I need the format to be "dd/mm/yyyy" and also implement custom validation logic for date input.
I am aware that locale can affect the format, but I am worried it might apply through out the app. Is there a way to customize the date format and validation logic in input mode or Restricted approach using Locale ?
Future<void> _selectDate(BuildContext context) async {
DateTime? selectedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2020),
lastDate: DateTime(2030),
builder: (context, child) {
return Theme(
data: Theme.of(context).copyWith(
inputDecorationTheme: const InputDecorationTheme(
hintText: "dd/mm/yyyy",
),
),
child: child!,
);
},
);
if (selectedDate != null) {
String formattedDate = DateFormat("dd/MM/yyyy").format(selectedDate);
_dateController.text = formattedDate;
}
}
In Flutter showDatePicker(material), when the user switches to input mode by clicking on the switchToInputEntryModeIcon, the default date format is "mm/dd/yyyy". However, I need the format to be "dd/mm/yyyy" and also implement custom validation logic for date input.
I am aware that locale can affect the format, but I am worried it might apply through out the app. Is there a way to customize the date format and validation logic in input mode or Restricted approach using Locale ?
Future<void> _selectDate(BuildContext context) async {
DateTime? selectedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2020),
lastDate: DateTime(2030),
builder: (context, child) {
return Theme(
data: Theme.of(context).copyWith(
inputDecorationTheme: const InputDecorationTheme(
hintText: "dd/mm/yyyy",
),
),
child: child!,
);
},
);
if (selectedDate != null) {
String formattedDate = DateFormat("dd/MM/yyyy").format(selectedDate);
_dateController.text = formattedDate;
}
}
Share Improve this question asked Feb 5 at 20:10 VENKAT RUTHVIK SAI KONDAVENKAT RUTHVIK SAI KONDA 1 2- 1 This question is similar to: how to change showDatePicker input format flutter. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – William Power Commented Feb 6 at 1:22
- That question relies on the Flutter Localization package. I need solution that achieves the same outcome without using any third-party packages. – VENKAT RUTHVIK SAI KONDA Commented Feb 6 at 19:33
1 Answer
Reset to default 1Have you tried setting the locale?
locale: const Locale('en', 'GB'), // GB changes it to "dd/MM/yyyy"
It goes right before builder. It will change it only for that date picker