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
This commit is contained in:
huycozy
2023-09-18 15:33:14 +07:00
committed by GitHub
parent b03f5337df
commit c2b1e483dd
2 changed files with 28 additions and 0 deletions

View File

@@ -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<example.ColorLabel>);
final Finder iconMenu = find.byType(DropdownMenu<example.IconLabel>);
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);
});
}