Repository navigation
[material_ui] Document how to programmatically clear DropdownMenu selection - #13266
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request adds a new code sample and widget tests demonstrating how to programmatically clear or reset the selection in a DropdownMenu using a TextEditingController. It also updates the documentation for DropdownMenu, its controller property, and its initialSelection property to explain this behavior and reference the new example. A pending changelog entry is also included. There are no review comments, and I have no feedback to provide.
ea69ee2 to
90aa5d2
Compare
| /// <callout-box> | ||
| /// | ||
| /// This sample demonstrates how to programmatically clear or reset the selection | ||
| /// in a [DropdownMenu] using a [TextEditingController]. | ||
| /// | ||
| // TODO(framework): Replace the following block with a @dartpad directive | ||
| // when it's supported. https://github.com/dart-lang/dartdoc/issues/4123 | ||
| /// {@macro material_ui.dartpad_guide} | ||
| /// | ||
| /// {@example /example/lib/dropdown_menu/dropdown_menu.3.dart#body} | ||
| /// | ||
| /// </callout-box> |
There was a problem hiding this comment.
nit: Since this sample specifically demonstrates controller and initialSelection and is already embedded in the doc comments for both of those properties below, could we omit this copy from the class-level doc comment so the generated API page doesn't render the same sample three times?
There was a problem hiding this comment.
Done, removed the class-level copy.
| /// Setting this to null does not clear the text field because [initialSelection] | ||
| /// only specifies an initial selection and does not act as a reactive, controlled | ||
| /// selection property. |
There was a problem hiding this comment.
didUpdateWidget actually does update the text controller when initialSelection changes to a value that matches an entry in dropdownMenuEntries (see lines 890–900); it only leaves the text field unchanged when initialSelection is null (or doesn't match any entry).
| /// Setting this to null does not clear the text field because [initialSelection] | |
| /// only specifies an initial selection and does not act as a reactive, controlled | |
| /// selection property. | |
| /// Setting this to null on a rebuild does not clear the text field, because | |
| /// [DropdownMenu] only updates the text field when [initialSelection] changes | |
| /// to a value that matches one of the [dropdownMenuEntries]. |
There was a problem hiding this comment.
Good catch, thanks! Updated to your suggested wording and aligned the controller docs with it.
|
|
||
| class _DropdownMenuClearExampleState extends State<DropdownMenuClearExample> { | ||
| final TextEditingController _controller = TextEditingController(); | ||
| ColorItem? _selectedColor; |
There was a problem hiding this comment.
nit: Consider initializing _selectedColor = ColorItem.red here (and updating the initial expectation in the test) so initialSelection: _selectedColor on line 68 also demonstrates clearing an initial selection present on startup.
There was a problem hiding this comment.
Done. The sample now starts with ColorItem.red, and I added a test that clears the initial selection.
488f829 to
9ebcbc7
Compare
…ection Improve documentation and add an interactive sample demonstrating how to programmatically clear or reset the selection in DropdownMenu using a TextEditingController alongside resetting the state variable. Fixes flutter/flutter#177599
… docs - Remove the duplicate clear-selection sample from the DropdownMenu class docs. It is already embedded in the controller and initialSelection docs, so the generated API page rendered the same sample three times. - Correct the initialSelection docs using the reviewer's suggested wording: didUpdateWidget does update the text field when initialSelection changes to a value that matches one of the dropdownMenuEntries, and only leaves it unchanged when the new value is null or matches no entry. - Align the controller docs with that wording by saying that setting initialSelection to null (rather than updating it) does not clear the text field. - Start the dropdown_menu.3 sample with ColorItem.red so it also demonstrates clearing an initial selection present on startup, update the first test's initial expectations, and add a test that clears the initial selection.
9ebcbc7 to
cd88635
Compare
…er#196381) flutter/packages@08faf64...653628e 2026-11-11 [email protected] Roll Flutter from fffd8c2 to b98afb5 (31 revisions) (flutter/packages#13623) 2026-11-11 [email protected] [material_ui] Document how to programmatically clear DropdownMenu selection (flutter/packages#13266) 2026-11-11 [email protected] [pigeon] Fix basePath resolution when paths share a string prefix with basePath (flutter/packages#13342) 2026-11-11 [email protected] [cupertino_ui] Fix CupertinoSheetRoute throwing when a drag ends during a pending relayout (flutter/packages#13316) 2026-11-11 [email protected] [shared_preferences] Updates Pigeon to 30.0.1 to support multi-engine registration and deregistration. (flutter/packages#13617) 2026-11-11 [email protected] [vector_graphics] Catch any throwable in useHtmlRenderObject() and cache HTML fallback (flutter/packages#13545) 2026-11-11 [email protected] [cupertino_ui] Include selectionHandleColor in NoDefaultCupertinoThemeData equality (flutter/packages#13450) 2026-11-11 [email protected] [flutter_svg] Remove failed async loads from Cache._pending (flutter/packages#13604) 2026-11-11 [email protected] [pigeon] Adds Native Interop FFI codec serialization for NS types (flutter/packages#13616) 2026-11-10 [email protected] [pigeon] Support multi-engine Native Interop registration and instance-aware unregistration (flutter/packages#13576) 2026-11-10 [email protected] [image_picker_ios] Convert the iOS example app and UI tests to Swift (flutter/packages#13570) 2026-11-10 [email protected] [cupertino_ui] Localize CupertinoActionSheet semantics label (flutter/packages#13377) 2026-11-10 [email protected] [camera_android] Fix stopVideoRecording returning JPEG when takePicture runs during recording (flutter/packages#13070) 2026-11-10 [email protected] [google_sign_in_android] Rename keep.xml to google_sign_in_keep.xml (flutter/packages#13475) 2026-11-10 [email protected] [web_benchmarks] Fix benchmark stalling when only repainting views that are dirty (flutter/packages#13089) 2026-11-10 [email protected] Roll Flutter from d463821 to fffd8c2 (21 revisions) (flutter/packages#13614) If this roll has caused a breakage, revert this CL and set the roller to dry run mode using the controls here: https://autoroll.skia.org/r/flutter-packages-flutter-autoroll Please CC [email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Description
Clarifies how to programmatically clear or reset the selection in
DropdownMenu.Setting initialSelection to null on a rebuild does not clear the text field, because DropdownMenu only updates the text field when initialSelection changes to a value that matches one of the dropdownMenuEntries. This PR documents that the selection should be cleared by providing a TextEditingController and calling controller.clear() alongside setting the state variable to null, and adds an interactive code sample demonstrating this pattern.
Fixes flutter/flutter#177599
Pre-Review Checklist
[shared_preferences]///).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-assistbot 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.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2