Skip to content

Commit 7e9ba43

Browse files
authored
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 flutter/flutter#123783 ## Tests Adds 1 test.
1 parent 055f6c1 commit 7e9ba43

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

packages/flutter/lib/src/material/dropdown.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,9 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
15261526
}
15271527

15281528
final EdgeInsetsGeometry padding =
1529-
ButtonTheme.of(context).alignedDropdown ? _kAlignedButtonPadding : _kUnalignedButtonPadding;
1529+
ButtonTheme.of(context).alignedDropdown && widget._inputDecoration == null
1530+
? _kAlignedButtonPadding
1531+
: _kUnalignedButtonPadding;
15301532

15311533
// If value is null (then _selectedIndex is null) then we
15321534
// display the hint or nothing at all.

packages/flutter/test/material/dropdown_form_field_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,4 +1253,45 @@ void main() {
12531253

12541254
expect(find.text('**Required**'), findsOneWidget);
12551255
});
1256+
1257+
testWidgets('ButtonTheme.alignedDropdown does not affect the field content position', (
1258+
WidgetTester tester,
1259+
) async {
1260+
Widget buildFrame({required bool alignedDropdown, required TextDirection textDirection}) {
1261+
return MaterialApp(
1262+
home: Directionality(
1263+
textDirection: textDirection,
1264+
child: ButtonTheme(
1265+
alignedDropdown: alignedDropdown,
1266+
child: Material(
1267+
child: DropdownButtonFormField<String>(
1268+
value: menuItems.first,
1269+
items:
1270+
menuItems.map((String value) {
1271+
return DropdownMenuItem<String>(value: value, child: Text(value));
1272+
}).toList(),
1273+
onChanged: onChanged,
1274+
),
1275+
),
1276+
),
1277+
),
1278+
);
1279+
}
1280+
1281+
final Finder findSelectedValue = find.text(menuItems.first).first;
1282+
1283+
await tester.pumpWidget(buildFrame(alignedDropdown: false, textDirection: TextDirection.ltr));
1284+
Rect contentRectForUnalignedDropdown = tester.getRect(findSelectedValue);
1285+
1286+
// When alignedDropdown is true, the content should be at the same position.
1287+
await tester.pumpWidget(buildFrame(alignedDropdown: true, textDirection: TextDirection.ltr));
1288+
expect(tester.getRect(findSelectedValue), contentRectForUnalignedDropdown);
1289+
1290+
await tester.pumpWidget(buildFrame(alignedDropdown: false, textDirection: TextDirection.rtl));
1291+
contentRectForUnalignedDropdown = tester.getRect(findSelectedValue);
1292+
1293+
// When alignedDropdown is true, the content should be at the same position.
1294+
await tester.pumpWidget(buildFrame(alignedDropdown: true, textDirection: TextDirection.rtl));
1295+
expect(tester.getRect(findSelectedValue), contentRectForUnalignedDropdown);
1296+
});
12561297
}

0 commit comments

Comments
 (0)