Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DropdownMenu TextField reactive to label changes #162062

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ahmedrasar
Copy link

@ahmedrasar ahmedrasar commented Jan 23, 2025

The current behavior of Flutter regarding DropdownMenu text field is very basic: DropdownMenu updates the text field once a selection has been made with its coordinating label, and nothing more.

If the selection's label changes, the text field will still show the old value, although the menu buttons have been updated.

This not only results in the need to write a lot of boilerplate code to match the label to the current selection, but it's also counterintuitive. The DropdownMenu should handle rematching by itself.

Issue #155660, touched on this, and a solution was introduced in #155757. It was later reverted due to it restricting some use cases. That issue & solution, however, only address initialSelection. If another selection was made and its corresponding label was changed, the text field would still show the old value, bringing us back to square one.

Proposal

We need to add a new variable to store additional information.

  • _lastLocalTextEditingValueObject: stores the last TextEditingValue that was created by making a new selection. This is necessary to not overwrite the text filed when modified by an external controller or searching.

Logic

This PR should not conflict with any previous behavior of DropdownMenu: any new value provided by initialSelection or via controller would not be overwritten by rematching. However, entries with the same value will be mapped to a single label (the first matched entry's label).

If no matching was found _lastLocalTextEditingValueObject would be set to null.

Related Issues

Tests

Added 3 tests.

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Jan 23, 2025
@ahmedrasar ahmedrasar force-pushed the feature_dropdown_menu_automatic_rematching branch 6 times, most recently from 492f6e4 to 2ad98b7 Compare January 26, 2025 00:00
@Piinks Piinks requested a review from QuncCccccc January 29, 2025 19:12
@ahmedrasar ahmedrasar force-pushed the feature_dropdown_menu_automatic_rematching branch from 2ad98b7 to c589b11 Compare February 3, 2025 17:39
@QuncCccccc
Copy link
Contributor

Sorry for the late response! I will take a look at this change asap!

@ahmedrasar
Copy link
Author

@QuncCccccc Is this PR still on the radar?

@QuncCccccc
Copy link
Contributor

Yes! Will take a look this week! Really appreciate your patience!

Copy link
Contributor

@QuncCccccc QuncCccccc left a comment

Choose a reason for hiding this comment

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

Really appreciated your patience and thanks so much for helping add the fix. I just added my thoughts and comments below:)

expect(controller.text, 'Updated label 1');
});

testWidgets('Do not rematch selection if its value maps to multiple entries', (
Copy link
Contributor

Choose a reason for hiding this comment

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

This may happen rarely because I think usually the entry values are unique? We can match the first matching value or do not rematch. I'm okay with either way:)

Copy link
Author

@ahmedrasar ahmedrasar Mar 12, 2025

Choose a reason for hiding this comment

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

We could match against the first entry only, and it would be the same behavior as initialSelection.

The only downfall would be not being able to show different labels for the same value. It doesn't seem like a big deal because it's not how we normally use 'DropdownMenu` but it may break what some people expect, and some golden tests too.

Edit:

I would opt for rematching the first occurrence of a valid label but keeping the flag and specifying such behavior in its description.

@ahmedrasar ahmedrasar force-pushed the feature_dropdown_menu_automatic_rematching branch from c589b11 to 54a0dc2 Compare March 16, 2025 21:57
@ahmedrasar ahmedrasar changed the title Introduce DropdownMenu automatic matching Make DropdownMenu TextField reactive to label changes Mar 16, 2025
@ahmedrasar ahmedrasar force-pushed the feature_dropdown_menu_automatic_rematching branch from 54a0dc2 to e583b75 Compare March 16, 2025 22:09
@ahmedrasar
Copy link
Author

ahmedrasar commented Mar 16, 2025

Hello @QuncCccccc, I made changes based on your notes, check 'em out.

@ahmedrasar ahmedrasar force-pushed the feature_dropdown_menu_automatic_rematching branch from e583b75 to 854c11a Compare March 18, 2025 13:08
@dkwingsmt dkwingsmt requested a review from QuncCccccc March 19, 2025 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DropdownMenu.didUpdateWidget should re-match initialSelection when dropdownMenuEntries have changed
2 participants