From c2b1e483ddd9733127e890b5ae2dcbf50037b6d7 Mon Sep 17 00:00:00 2001 From: huycozy <104349824+huycozy@users.noreply.github.com> Date: Mon, 18 Sep 2023 15:33:14 +0700 Subject: [PATCH] Improve DropdownMenu sample code for requestFocusOnTap on mobile platforms (#134867) ### Description This PR is to improve `DropdownMenu` sample code. By default, `requestFocusOnTap` is false on mobile platforms. When users run API sample code on mobile platforms, they can not edit the text field and think it is a bug. Although it is detailed at https://api.flutter.dev/flutter/material/DropdownMenu/requestFocusOnTap.html, users often do not pay attention to it. ### Related issue Fixes https://github.com/flutter/flutter/issues/127672 --- .../dropdown_menu/dropdown_menu.0.dart | 6 +++++ .../dropdown_menu/dropdown_menu.0_test.dart | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/examples/api/lib/material/dropdown_menu/dropdown_menu.0.dart b/examples/api/lib/material/dropdown_menu/dropdown_menu.0.dart index 0e1814b020..e7b1ea9b3d 100644 --- a/examples/api/lib/material/dropdown_menu/dropdown_menu.0.dart +++ b/examples/api/lib/material/dropdown_menu/dropdown_menu.0.dart @@ -74,6 +74,11 @@ class _DropdownMenuExampleState extends State { DropdownMenu( initialSelection: ColorLabel.green, controller: colorController, + // requestFocusOnTap is enabled/disabled by platforms when it is null. + // On mobile platforms, this is false by default. Setting this to true will + // trigger focus request on the text field and virtual keyboard will appear + // afterward. On desktop platforms however, this defaults to true. + requestFocusOnTap: true, label: const Text('Color'), onSelected: (ColorLabel? color) { setState(() { @@ -97,6 +102,7 @@ class _DropdownMenuExampleState extends State { DropdownMenu( controller: iconController, enableFilter: true, + requestFocusOnTap: true, leadingIcon: const Icon(Icons.search), label: const Text('Icon'), inputDecorationTheme: const InputDecorationTheme( diff --git a/examples/api/test/material/dropdown_menu/dropdown_menu.0_test.dart b/examples/api/test/material/dropdown_menu/dropdown_menu.0_test.dart index 62e0c6e195..c1c662f0ec 100644 --- a/examples/api/test/material/dropdown_menu/dropdown_menu.0_test.dart +++ b/examples/api/test/material/dropdown_menu/dropdown_menu.0_test.dart @@ -52,4 +52,26 @@ void main() { expect(find.text('You selected a Blue Smile'), findsOneWidget); }); + + testWidgets('DropdownMenu has focus when tapping on the text field', (WidgetTester tester) async { + await tester.pumpWidget( + const example.DropdownMenuExample(), + ); + + // Make sure the dropdown menus are there. + final Finder colorMenu = find.byType(DropdownMenu); + final Finder iconMenu = find.byType(DropdownMenu); + expect(colorMenu, findsOneWidget); + expect(iconMenu, findsOneWidget); + + // Tap on the color menu and make sure it is focused. + await tester.tap(colorMenu); + await tester.pumpAndSettle(); + expect(FocusScope.of(tester.element(colorMenu)).hasFocus, isTrue); + + // Tap on the icon menu and make sure it is focused. + await tester.tap(iconMenu); + await tester.pumpAndSettle(); + expect(FocusScope.of(tester.element(iconMenu)).hasFocus, isTrue); + }); }