mirror of
https://github.com/flutter/flutter.git
synced 2026-01-20 20:55:29 +08:00
## Description This PR adds `DropdownMenu.selectOnly`. This property allows users to get the DropdownMenu behave as a 'select' component. It is meant as a future replacement for `DropdownMenu.requestFocusOnTap`. ## Motivation On mobile, a dropdown menu widget is usually used as a ‘select’ widget because opening the keyboard for searching/filtering is not convenient. In Flutter, this is currently implemented through `DropdownMenu.requestFocusOnTap` which defaults to false on mobile and true on desktop. The `DropdownMenu.requestFocusOnTap` property is currently used to set `FocusNode.canRequestFocus`. This leads do difficulties mainly related to focus traversal: - Keyboard traversal requires workarounds (for instance relying on the trailing icon to be focusable). - Keyboard shortcuts require also a workaround (currently relying on a Focus widget in a Stack). - The `DropdownMenu` decoration does not reflect the focus state. This PR proposes a new property named `DropdownMenu.selectOnly` which does not require `DropdownMenu.requestFocusOnTap` to be false to make the `DropdownMenu` behave like a select widget. With this property the `DropdownMenu`: - Supports keyboard navigation on mobile and desktop. - Has a correct decoration when focused. - Does not rely on the trailing icon to be focusable (see https://github.com/flutter/flutter/issues/174096). In the future this property could be used as a replacement for `DropdownMenu.requestFocusOnTap`. For the moment, for compatibility, it does not replace `DropdownMenu.requestFocusOnTap`. ## Related Issue Fixes [Allow DropdownMenu to be non-editable and focusable (select control) ](https://github.com/flutter/flutter/issues/178009) Also related to [Make DropdownMenu's trailing icon not focusable by default](https://github.com/flutter/flutter/issues/174096) and [[Material3] DropdownMenu Keyboard Accessibility](https://github.com/flutter/flutter/issues/123797). ## Tests - Adds 9 tests.