From 17ad05279222db58b4f5f95781efcf871c3cc0a6 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Wed, 21 Aug 2024 21:30:39 +0300 Subject: [PATCH] Make `TextField` read only when the text input is disabled by setting `DropdownMenu.requestFocusOnTap` to `false` (#153566) Fixes [[DropdownMenu] Semantics still reads out "EditBox" when text editing is disabled](https://github.com/flutter/flutter/issues/151686) ### Code sample
expand to view the code sample ```dart import 'package:flutter/material.dart'; void main() { runApp(const DropdownMenuExample()); } enum ColorLabel { blue('Blue', Colors.blue), pink('Pink', Colors.pink), green('Green', Colors.green), yellow('Orange', Colors.orange), grey('Grey', Colors.grey); const ColorLabel(this.label, this.color); final String label; final Color color; } class DropdownMenuExample extends StatefulWidget { const DropdownMenuExample({super.key}); @override State createState() => _DropdownMenuExampleState(); } class _DropdownMenuExampleState extends State { ColorLabel? selectedColor; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: SafeArea( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, spacing: 16.0, children: [ const ExcludeSemantics(child: Text('TexField')), const TextField( readOnly: true, decoration: InputDecoration( labelText: 'Color', border: OutlineInputBorder(), ), ), const ExcludeSemantics(child: Text('DropdownMenu')), DropdownMenu( requestFocusOnTap: false, label: const Text('Color'), dropdownMenuEntries: ColorLabel.values .map>((ColorLabel color) { return DropdownMenuEntry( value: color, label: color.label, style: MenuItemButton.styleFrom( foregroundColor: color.color, ), ); }).toList(), ), ], ), ), ), ), ); } } ```
### Preview (with Talkback captions) |Before | After | | --------------- | --------------- | | | | ### Before demo DropdownMenu announces "EditBox" and "double tap to activate" when setting `DropdownMenu.requestFocusOnTap` to `false` shouldn't allow that https://github.com/user-attachments/assets/f692a4f6-9d6a-4834-8df0-baf6c65fae29 ### After demo When setting `DropdownMenu.requestFocusOnTap` to `false`, the underlying `TextField` gets read only state which changes the semantics to be not editable or focusable and remove"EditBox" and "double tap to activate" announcments https://github.com/user-attachments/assets/0e14a636-6b81-4535-a5d1-c8f301c4f89e --- .../lib/src/material/dropdown_menu.dart | 1 + .../test/material/dropdown_menu_test.dart | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+) 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 {