Skip to content

Support custom date input formatters via CalendarDelegate - #181105

Closed
Mairramer wants to merge 18 commits into
flutter:masterfrom
Mairramer:feature/create-date-input-delegate
Closed

Mairramer wants to merge 18 commits into
flutter:masterfrom
Mairramer:feature/create-date-input-delegate

Conversation

@Mairramer

@Mairramer Mairramer commented Jan 17, 2026 •

Copy link
Copy Markdown
Contributor

This PR adds the keyboardInputFormatters method to CalendarDelegate.

Previously, date input was strictly coupled to MaterialLocalizations. This change allows developers to provide a list of TextInputFormatters via a custom CalendarDelegate to enforce specific input masks (e.g., dd/MM/yyyy) or restrict character entry in date pickers (showDatePicker, showDateRangePicker, and InputDatePickerFormField).

Fixes #83302
Fixes #97134
Fixes #62401
Fixes #181043
Fixes #115151
Fixes partially #150611

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos labels Jan 17, 2026
@Mairramer Mairramer changed the title 【WIP】Add DateInputCalendarDelegate to customize date input Add DateInputCalendarDelegate to customize date input Jan 23, 2026
@Mairramer
Mairramer marked this pull request as ready for review January 23, 2026 16:15
@Mairramer

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces DateInputCalendarDelegate, which allows for customizable date input formatting in showDatePicker and showDateRangePicker. The changes include a new example demonstrating its usage, updates to core date picker files to integrate the new delegate, and corresponding tests. The implementation appears solid, providing a flexible extension point for date input.

Comment thread examples/api/lib/material/date_picker/show_date_picker.2.dart
Comment thread packages/flutter/lib/src/material/date.dart Outdated
@Mairramer
Mairramer force-pushed the feature/create-date-input-delegate branch from 787eb5a to 25522f5 Compare February 3, 2026 20:32
@Mairramer
Mairramer force-pushed the feature/create-date-input-delegate branch from 4cba849 to 4583052 Compare February 16, 2026 17:06
@Mairramer
Mairramer force-pushed the feature/create-date-input-delegate branch from 4583052 to c9c9dd2 Compare February 23, 2026 17:16

@dkwingsmt dkwingsmt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late feedback.

I have questions about the API design. I think it would be a better idea so that the inputFormatters be a property of DatePicker instead of the delegate. A couple of reasons:

  • Runtime type check like this signals bad class design, which I'd prefer avoiding if possible.
  • Semantically, non-gregorian calendars will also want to format their text.
  • It also feels unnecessary that people have to extend the entire delegate to define a new formatter. The two aspects feel independent.

Otherwise, great code quality, and fantastic example app. Thank you!

@Mairramer

Copy link
Copy Markdown
Contributor Author

Sorry for the late feedback.

I have questions about the API design. I think it would be a better idea so that the inputFormatters be a property of DatePicker instead of the delegate. A couple of reasons:

  • Runtime type check like this signals bad class design, which I'd prefer avoiding if possible.
  • Semantically, non-gregorian calendars will also want to format their text.
  • It also feels unnecessary that people have to extend the entire delegate to define a new formatter. The two aspects feel independent.

Otherwise, great code quality, and fantastic example app. Thank you!

Those points are definitely valid. The use of a delegate was mainly because MaterialLocalizations is used internally to validate and display helper text, labels, etc., which is why I went with that approach.

That said, I could refactor this into a class where I pass the inputFormatters. In this case, I would only need three things: helpText, format, and parse.

@Mairramer

Copy link
Copy Markdown
Contributor Author

/gemini review

@Mairramer
Mairramer requested a review from dkwingsmt March 25, 2026 12:56

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a DateInputDelegate to allow custom date input formatting, parsing, and validation for showDatePicker and showDateRangePicker. A new example demonstrates how to use this delegate for configurable date input formats and separators. The review comments suggest updating deprecated FilteringTextInputFormatter.digitsOnly to FilteringTextInputFormatter.allow(RegExp(r'[0-9]')) in both the example and tests, adding missing documentation comments for the dateInputDelegate fields in DatePickerDialog and _InputDateRangePicker as per the style guide, and correcting a typo in the test delegate's help text from aaaammdd to yyyymmdd.

Comment thread examples/api/lib/material/date_picker/show_date_picker.2.dart Outdated
Comment thread packages/flutter/lib/src/material/date_picker.dart Outdated
Comment thread packages/flutter/lib/src/material/date_picker.dart Outdated
Comment thread packages/flutter/test/material/date_picker_test.dart Outdated
Comment thread packages/flutter/test/material/date_picker_test.dart Outdated
@Mairramer Mairramer changed the title Add DateInputCalendarDelegate to customize date input Add DateInputDelegate to customize date input Mar 25, 2026
@Mairramer
Mairramer requested a review from dkwingsmt March 28, 2026 19:14

@nate-thegrate nate-thegrate left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good!


Quick heads-up: I currently don't have commit access in this repo so I won't be able to give an approval.

If you're interested in making progress more quickly, I have a suggestion—if you have a chance, would you be able to look through #184283? It'd be super helpful if you could describe what's different between this PR and that one, so we can decide which PR to move forward with.

Comment thread examples/api/lib/material/date_picker/show_date_picker.2.dart Outdated
Comment thread examples/api/lib/material/date_picker/show_date_picker.2.dart Outdated
Comment thread packages/flutter/test/material/date_picker_test.dart Outdated
Comment thread packages/flutter/test/material/date_picker_test.dart Outdated
@Mairramer

Copy link
Copy Markdown
Contributor Author

This looks really good!

Quick heads-up: I currently don't have commit access in this repo so I won't be able to give an approval.

If you're interested in making progress more quickly, I have a suggestion—if you have a chance, would you be able to look through #184283? It'd be super helpful if you could describe what's different between this PR and that one, so we can decide which PR to move forward with.

Originally, when I opened this PR back in mid-January, my idea was to introduce a new CalendarDelegate (as seen in the initial commits) extending GregorianCalendarDelegate. However, during the review by @dkwingsmt and after revisiting the approach myself, I realized this wasn’t the best direction at the moment.

So instead, I introduced inputFormatters, which already covers part of what we, as users, need.

I’m still exploring a way to avoid requiring users to create a full custom formatter and extend the delegate just for formatting, but that should come after further decoupling.

@dkwingsmt

Copy link
Copy Markdown
Contributor

I agree. The tight coupling between formatter and parser means more work is needed than this, but this is a good start - and a much needed design for the best customizability. In the future we can discuss other ways that provide common presets based on this API.

@dkwingsmt dkwingsmt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks good to me, but I found two problems while local testing the example (I understand that the logic here is really delicate and complicated).

Overall really good quality. Thank you very much for your contribution!

final String formatted = _applyMask(digits);
return TextEditingValue(
text: formatted,
selection: TextSelection.collapsed(offset: formatted.length),

@dkwingsmt dkwingsmt Apr 1, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This offset will cause the text cursor to always be moved to the end of the text, which isn't expected if the user is editing at the middle of the string, which is reproducible in my local testing.

The following video recording includes two segments:

  • The first half shows pressing Delete in the middle of a string that's not immediately after a separator.
  • The second half shows pressing Delete in the middle of a string that's immediately after a separator.
Screen.Recording.2026-04-01.at.2.06.00.PM.mov

Comment thread packages/flutter/lib/src/material/input_date_picker_form_field.dart Outdated
@Mairramer
Mairramer requested a review from dkwingsmt April 2, 2026 20:44
@dkwingsmt

Copy link
Copy Markdown
Contributor

I can confirm that editing in the middle of the string no longer resets the caret to the end of the string, although pressing backspace immediately after a separator (for example, pressing backspace at 12.25.|2025) is still noop. I don't think this is a major issue so I'm going to approve this PR. Feel free to decide whether you want to continue polishing this PR while I look for a second reviewer. Thank you!

dkwingsmt
dkwingsmt previously approved these changes Apr 8, 2026

@dkwingsmt dkwingsmt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dkwingsmt
dkwingsmt requested a review from victorsanni April 8, 2026 07:06
@victorsanni

Copy link
Copy Markdown
Contributor

Thanks for your contribution! This PR has reached completion, however we're not able to merge this PR at the moment due to the Material/Cupertino code freeze (#184093). As soon as the decoupling is complete, we will post a way to port this PR to the new packages to land. In the meantime, I'll convert this PR to a draft and apply a label so that we can quickly find it when the freeze is lifted.

@victorsanni
victorsanni marked this pull request as draft April 15, 2026 00:20
@victorsanni victorsanni added the waiting for code freeze This PR is waiting for a code freeze to resolve. label Apr 15, 2026
Comment thread packages/flutter/lib/src/material/date.dart Outdated
@Mairramer Mairramer changed the title Add input formatters to customize date input Support custom date input formatters via CalendarDelegate Jun 14, 2026
@Piinks Piinks added the Decoupling: Not ready to port yet Instructions will be provided when this is ready to move to flutter/packages. label Jun 24, 2026
@Piinks

Piinks commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

I've marked this PR as not ready to port to flutter/packages yet.
We'll provide instructions to move this change over to material_ui/cupertino_ui once ready to receive PRs. Thank you!

@Piinks Piinks added Decoupling: Port to flutter/packages This PR is ready to be ported to flutter/packages. We will provide instructions to do so. and removed Decoupling: Not ready to port yet Instructions will be provided when this is ready to move to flutter/packages. labels Aug 18, 2026
@Piinks

Piinks commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

material_ui and cupertino_ui are now accepting contributions. :)

This PR can now be ported over to flutter/packages!

Guidance and instructions for easily porting over can be found in #188444

@Mairramer

Mairramer commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor Author

@dkwingsmt Ported to flutter/packages#12608. It will stay in draft due to the 2 PR limit.

@Mairramer Mairramer closed this Aug 22, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Sep 10, 2026
…te (#12608)

This PR adds the `keyboardInputFormatters` method to `CalendarDelegate`.

Previously, date input was strictly coupled to `MaterialLocalizations`. This change allows developers to provide a list of `TextInputFormatter`s via a custom `CalendarDelegate` to enforce specific input masks (e.g., dd/MM/yyyy) or restrict character entry in date pickers (`showDatePicker`, `showDateRangePicker`, and `InputDatePickerFormField`).

Fixes flutter/flutter#83302
Fixes flutter/flutter#97134 
Fixes flutter/flutter#62401
Fixes flutter/flutter#181043
Fixes flutter/flutter#115151
Fixes flutter/flutter#191639
Fixes partially flutter/flutter#150611

Port of flutter/flutter#181105

## Pre-Review Checklist

**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos Decoupling: Port to flutter/packages This PR is ready to be ported to flutter/packages. We will provide instructions to do so. framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages waiting for code freeze This PR is waiting for a code freeze to resolve.

Projects

None yet

6 participants