diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 8ae003a424..97e89fd40b 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -830,6 +830,7 @@ class _DropdownMenuState extends State> { focusNode: widget.focusNode, canRequestFocus: canRequestFocus(), enableInteractiveSelection: canRequestFocus(), + readOnly: !canRequestFocus(), keyboardType: widget.keyboardType, textAlign: widget.textAlign, textAlignVertical: TextAlignVertical.center, diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index 98b04eefa0..c285513e3a 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -2442,6 +2442,7 @@ void main() { MaterialApp( home: Scaffold( body: DropdownMenu( + requestFocusOnTap: true, controller: controller, dropdownMenuEntries: const >[ DropdownMenuEntry( @@ -2675,6 +2676,53 @@ void main() { expect(find.widgetWithText(MenuItemButton, menu.label), findsNWidgets(2)); } }); + + // This is a regression test for https://github.com/flutter/flutter/issues/151686. + testWidgets('Setting DropdownMenu.requestFocusOnTap to false makes TextField read only', (WidgetTester tester) async { + const String label = 'Test'; + Widget buildDropdownMenu({ bool? requestFocusOnTap }) { + return MaterialApp( + home: Scaffold( + body: Center( + child: DropdownMenu( + requestFocusOnTap: requestFocusOnTap, + dropdownMenuEntries: menuChildren, + hintText: label, + ), + ), + ), + ); + } + await tester.pumpWidget(buildDropdownMenu(requestFocusOnTap: true)); + + expect( + tester.getSemantics(find.byType(TextField)), + matchesSemantics( + hasFocusAction: true, + hasTapAction: true, + isTextField: true, + hasEnabledState: true, + isEnabled: true, + label: 'Test', + textDirection: TextDirection.ltr, + ), + ); + + await tester.pumpWidget(buildDropdownMenu(requestFocusOnTap: false)); + + expect( + tester.getSemantics(find.byType(TextField)), + matchesSemantics( + hasFocusAction: true, + isTextField: true, + hasEnabledState: true, + isEnabled: true, + label: 'Test', + isReadOnly: true, + textDirection: TextDirection.ltr, + ), + ); + }); } enum TestMenu {