Fix Semantics expanded state not updating in PopupMenuButton and DropdownButton#183475
Conversation
There was a problem hiding this comment.
Code Review
This pull request addresses an issue where the expanded state in the Semantics widget was not updating for PopupMenuButton and DropdownButton. The changes wrap the assignments to the _isMenuExpanded state variable within setState calls in both widgets. This ensures that the UI rebuilds to reflect the menu's expanded or collapsed state for accessibility services. For DropdownButton, the logic to set _isMenuExpanded to false upon menu closure has been moved from _removeDropdownRoute into the .then() callback of navigator.push, guarded by a mounted check. This prevents unnecessary setState calls during widget disposal or orientation changes. Regression tests have been added for both PopupMenuButton and DropdownButton to confirm that the expanded semantics state updates correctly.
Note: Security Review is unavailable for this PR.
QuncCccccc
left a comment
There was a problem hiding this comment.
LGTM. Thanks for your contribution:)!
7ab0319 to
cc69a93
Compare
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…n and DropdownButton (flutter/flutter#183475)
…downButton (flutter#183475) Fixes flutter#183432 ## Description `_isMenuExpanded` is updated without calling `setState()` in both `PopupMenuButton` and `DropdownButton`, so the `Semantics(expanded: _isMenuExpanded)` widget doesn't rebuild. **`PopupMenuButton`**: The `expanded` state never updates at all since there is no `setState()` call anywhere in `PopupMenuButtonState`. **`DropdownButton`**: Currently masked by `_handleFocusChanged`, which happens to call `setState()` whenever focus changes on open/close, but should not rely on this side effect. ### Changes - Wrapped `_isMenuExpanded` assignments with `setState()` in both widgets. - For `DropdownButton`, moved `_isMenuExpanded = false` from `_removeDropdownRoute()` to the `.then()` callback with a `mounted` guard. This avoids unnecessary `setState()` calls when `_removeDropdownRoute()` is invoked during `dispose()` or orientation changes in `build()`, where the menu state isn't actually changing from user interaction. ## Tests Added regression tests for both `PopupMenuButton` and `DropdownButton` to verify the `expanded` semantics state updates correctly when the menu opens and closes. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
…downButton (flutter#183475) Fixes flutter#183432 ## Description `_isMenuExpanded` is updated without calling `setState()` in both `PopupMenuButton` and `DropdownButton`, so the `Semantics(expanded: _isMenuExpanded)` widget doesn't rebuild. **`PopupMenuButton`**: The `expanded` state never updates at all since there is no `setState()` call anywhere in `PopupMenuButtonState`. **`DropdownButton`**: Currently masked by `_handleFocusChanged`, which happens to call `setState()` whenever focus changes on open/close, but should not rely on this side effect. ### Changes - Wrapped `_isMenuExpanded` assignments with `setState()` in both widgets. - For `DropdownButton`, moved `_isMenuExpanded = false` from `_removeDropdownRoute()` to the `.then()` callback with a `mounted` guard. This avoids unnecessary `setState()` calls when `_removeDropdownRoute()` is invoked during `dispose()` or orientation changes in `build()`, where the menu state isn't actually changing from user interaction. ## Tests Added regression tests for both `PopupMenuButton` and `DropdownButton` to verify the `expanded` semantics state updates correctly when the menu opens and closes. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
Fixes #183432
Description
_isMenuExpandedis updated without callingsetState()in bothPopupMenuButtonandDropdownButton, so theSemantics(expanded: _isMenuExpanded)widget doesn't rebuild.PopupMenuButton: Theexpandedstate never updates at all since there is nosetState()call anywhere inPopupMenuButtonState.DropdownButton: Currently masked by_handleFocusChanged, which happens to callsetState()whenever focus changes on open/close, but should not rely on this side effect.Changes
_isMenuExpandedassignments withsetState()in both widgets.DropdownButton, moved_isMenuExpanded = falsefrom_removeDropdownRoute()to the.then()callback with amountedguard. This avoids unnecessarysetState()calls when_removeDropdownRoute()is invoked duringdispose()or orientation changes inbuild(), where the menu state isn't actually changing from user interaction.Tests
Added regression tests for both
PopupMenuButtonandDropdownButtonto verify theexpandedsemantics state updates correctly when the menu opens and closes.Pre-launch Checklist
///).