From 09f7d7c56043037895470fbe879fdfc0baf2ded9 Mon Sep 17 00:00:00 2001 From: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com> Date: Fri, 22 Sep 2023 15:34:06 +0800 Subject: [PATCH] Remove extra padding if a dropdown menu entry also has a leading icon (#135004) Fixes #131350 This PR is to remove the extra padding in `DropdownMenuEntry` if both the text field and the dropdown menu entry have leading icons. **After** fix: Screenshot 2023-09-19 at 4 35 24 PM **Before** fix: Screenshot 2023-09-19 at 4 37 58 PM --- .../lib/src/material/dropdown_menu.dart | 33 +++++++++++------- .../test/material/dropdown_menu_test.dart | 34 +++++++++++++++++++ 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 3b80170fca..51b17e5b2c 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -432,21 +432,28 @@ class _DropdownMenuState extends State> { { int? focusedIndex, bool enableScrollToHighlight = true} ) { final List result = []; - final double padding = leadingPadding ?? _kDefaultHorizontalPadding; - final ButtonStyle defaultStyle; - switch (textDirection) { - case TextDirection.rtl: - defaultStyle = MenuItemButton.styleFrom( - padding: EdgeInsets.only(left: _kDefaultHorizontalPadding, right: padding), - ); - case TextDirection.ltr: - defaultStyle = MenuItemButton.styleFrom( - padding: EdgeInsets.only(left: padding, right: _kDefaultHorizontalPadding), - ); - } - for (int i = 0; i < filteredEntries.length; i++) { final DropdownMenuEntry entry = filteredEntries[i]; + + // By default, when the text field has a leading icon but a menu entry doesn't + // have one, the label of the entry should have extra padding to be aligned + // with the text in the text input field. When both the text field and the + // menu entry have leading icons, the menu entry should remove the extra + // paddings so its leading icon will be aligned with the leading icon of + // the text field. + final double padding = entry.leadingIcon == null ? (leadingPadding ?? _kDefaultHorizontalPadding) : _kDefaultHorizontalPadding; + final ButtonStyle defaultStyle; + switch (textDirection) { + case TextDirection.rtl: + defaultStyle = MenuItemButton.styleFrom( + padding: EdgeInsets.only(left: _kDefaultHorizontalPadding, right: padding), + ); + case TextDirection.ltr: + defaultStyle = MenuItemButton.styleFrom( + padding: EdgeInsets.only(left: padding, right: _kDefaultHorizontalPadding), + ); + } + ButtonStyle effectiveStyle = entry.style ?? defaultStyle; final Color focusedBackgroundColor = effectiveStyle.foregroundColor?.resolve({MaterialState.focused}) ?? Theme.of(context).colorScheme.onSurface; diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index a499bcd262..6b142e5c90 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -1680,6 +1680,40 @@ void main() { await tester.pumpAndSettle(); expect(controller.text, ''); // nothing selected }); + + // Regression test for https://github.com/flutter/flutter/issues/131350. + testWidgets('DropdownMenuEntry.leadingIcon default layout', (WidgetTester tester) async { + // The DropdownMenu should not get extra padding in DropdownMenuEntry items + // when both text field and DropdownMenuEntry have leading icons. + await tester.pumpWidget(const MaterialApp( + home: Scaffold( + body: DropdownMenu( + leadingIcon: Icon(Icons.search), + hintText: 'Hint', + dropdownMenuEntries: >[ + DropdownMenuEntry( + value: 0, + label: 'Item 0', + leadingIcon: Icon(Icons.alarm) + ), + DropdownMenuEntry(value: 1, label: 'Item 1'), + ], + ), + ) + )); + await tester.tap(find.byType(DropdownMenu)); + await tester.pumpAndSettle(); + + // Check text location in text field. + expect(tester.getTopLeft(find.text('Hint')).dx, 48.0); + + // By default, the text of item 0 should be aligned with the text of the text field. + expect(tester.getTopLeft(find.text('Item 0').last).dx, 48.0); + + // By default, the text of item 1 should be aligned with the text of the text field, + // so there are some extra padding before "Item 1". + expect(tester.getTopLeft(find.text('Item 1').last).dx, 48.0); + }); } enum TestMenu {