From 7e9ba43698cdde40f452e8fbddf7cf6f8bd03376 Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Thu, 13 Feb 2025 01:38:19 +0100 Subject: [PATCH] Fix DropdownButtonFormField padding when ButtonTheme.alignedDropdown is true (#162810) ## Description This PR fixes `DropdownButtonFormField` content padding when `ButtonTheme.alignedDropdown` is true. ### Before: An extra padding is added before the content when `alignedDropdown` is true: ![image](https://github.com/user-attachments/assets/429aa848-c56a-4e74-aca3-6c9eb9507578) ### After: The content has the same position whether `alignedDropdown` is true or false: ![image](https://github.com/user-attachments/assets/2476194d-b582-4991-9285-d5c94af86f6a) ## Related Issue Fixes https://github.com/flutter/flutter/issues/123783 ## Tests Adds 1 test. --- .../flutter/lib/src/material/dropdown.dart | 4 +- .../material/dropdown_form_field_test.dart | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index e9b32eb037..975b7e7876 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -1526,7 +1526,9 @@ class _DropdownButtonState extends State> with WidgetsBindi } final EdgeInsetsGeometry padding = - ButtonTheme.of(context).alignedDropdown ? _kAlignedButtonPadding : _kUnalignedButtonPadding; + ButtonTheme.of(context).alignedDropdown && widget._inputDecoration == null + ? _kAlignedButtonPadding + : _kUnalignedButtonPadding; // If value is null (then _selectedIndex is null) then we // display the hint or nothing at all. diff --git a/packages/flutter/test/material/dropdown_form_field_test.dart b/packages/flutter/test/material/dropdown_form_field_test.dart index 554e4f43ac..4282aaf747 100644 --- a/packages/flutter/test/material/dropdown_form_field_test.dart +++ b/packages/flutter/test/material/dropdown_form_field_test.dart @@ -1253,4 +1253,45 @@ void main() { expect(find.text('**Required**'), findsOneWidget); }); + + testWidgets('ButtonTheme.alignedDropdown does not affect the field content position', ( + WidgetTester tester, + ) async { + Widget buildFrame({required bool alignedDropdown, required TextDirection textDirection}) { + return MaterialApp( + home: Directionality( + textDirection: textDirection, + child: ButtonTheme( + alignedDropdown: alignedDropdown, + child: Material( + child: DropdownButtonFormField( + value: menuItems.first, + items: + menuItems.map((String value) { + return DropdownMenuItem(value: value, child: Text(value)); + }).toList(), + onChanged: onChanged, + ), + ), + ), + ), + ); + } + + final Finder findSelectedValue = find.text(menuItems.first).first; + + await tester.pumpWidget(buildFrame(alignedDropdown: false, textDirection: TextDirection.ltr)); + Rect contentRectForUnalignedDropdown = tester.getRect(findSelectedValue); + + // When alignedDropdown is true, the content should be at the same position. + await tester.pumpWidget(buildFrame(alignedDropdown: true, textDirection: TextDirection.ltr)); + expect(tester.getRect(findSelectedValue), contentRectForUnalignedDropdown); + + await tester.pumpWidget(buildFrame(alignedDropdown: false, textDirection: TextDirection.rtl)); + contentRectForUnalignedDropdown = tester.getRect(findSelectedValue); + + // When alignedDropdown is true, the content should be at the same position. + await tester.pumpWidget(buildFrame(alignedDropdown: true, textDirection: TextDirection.rtl)); + expect(tester.getRect(findSelectedValue), contentRectForUnalignedDropdown); + }); }