最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - How to Change Date Format to "ddmmyyyy" from "mmddyyyy" in showDatePicker Input Mo

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

Have 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论