Skip to content

Commit b0ae3b2

Browse files
authored
[material_ui] Fix DropdownButtonFormField underline alignment at bottom (flutter#12606)
This PR fixes the visual issue where the underline of a `DropdownButtonFormField` did not align at the bottom of its expanded container. Fixes flutter/flutter#162350 Port of flutter/flutter#181040. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 749a38f commit b0ae3b2

4 files changed

Lines changed: 76 additions & 1 deletion

File tree

‎packages/material_ui/lib/src/dropdown.dart‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ class DropdownButton<T> extends StatefulWidget {
10451045
'with the same value',
10461046
),
10471047
assert(itemHeight == null || itemHeight >= kMinInteractiveDimension),
1048+
isVerticallyExpanded = true,
10481049
_inputDecoration = null,
10491050
_isEmpty = false;
10501051

@@ -1077,6 +1078,7 @@ class DropdownButton<T> extends StatefulWidget {
10771078
this.barrierDismissible = true,
10781079
this.mouseCursor,
10791080
this.dropdownMenuItemMouseCursor,
1081+
this.isVerticallyExpanded = false,
10801082
required this._inputDecoration,
10811083
required this._isEmpty,
10821084
}) : underline = null,
@@ -1250,6 +1252,10 @@ class DropdownButton<T> extends StatefulWidget {
12501252
/// By default this button's inner width is the minimum size of its contents.
12511253
/// If [isExpanded] is true, the inner width is expanded to fill its
12521254
/// surrounding container.
1255+
/// See also:
1256+
///
1257+
/// * [DropdownButton.isVerticallyExpanded], which expands the inner height of the
1258+
/// dropdown to fill the available vertical space.
12531259
final bool isExpanded;
12541260

12551261
/// If null, then the menu item heights will vary according to each menu item's
@@ -1356,6 +1362,26 @@ class DropdownButton<T> extends StatefulWidget {
13561362
/// If this property is null, [WidgetStateMouseCursor.adaptiveClickable] will be used.
13571363
final MouseCursor? dropdownMenuItemMouseCursor;
13581364

1365+
/// Whether the dropdown's inner contents expand to fill the
1366+
/// available vertical space.
1367+
///
1368+
/// When true, the inner height of the dropdown is expanded to match its
1369+
/// parent container. When false, the inner height is determined by the
1370+
/// height of the selected item (its intrinsic height).
1371+
///
1372+
/// This is particularly useful when the dropdown is used inside a fixed-height
1373+
/// container, ensuring that the underline and items align properly within
1374+
/// the allocated space.
1375+
///
1376+
/// Defaults to false for [DropdownButtonFormField] to ensure the input decorator
1377+
/// handles the vertical constraints correctly by default.
1378+
///
1379+
/// See also:
1380+
///
1381+
/// * [DropdownButton.isExpanded], which expands the inner width of the
1382+
/// dropdown to fill the available horizontal space.
1383+
final bool isVerticallyExpanded;
1384+
13591385
final InputDecoration? _inputDecoration;
13601386

13611387
final bool _isEmpty;
@@ -1766,13 +1792,21 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
17661792
isEmpty: widget._isEmpty,
17671793
isFocused: _hasPrimaryFocus,
17681794
isHovering: _isHovering,
1795+
expands: widget.isVerticallyExpanded,
17691796
child: widget.padding == null
17701797
? result
17711798
: Padding(padding: widget.padding!, child: result),
17721799
),
17731800
),
17741801
),
17751802
);
1803+
1804+
// When vertical expansion is disabled, wrap with Align
1805+
// to prevent the widget from stretching to fill the parent's
1806+
// available height.
1807+
if (!widget.isVerticallyExpanded) {
1808+
result = Align(alignment: AlignmentDirectional.topStart, heightFactor: 1.0, child: result);
1809+
}
17761810
} else {
17771811
result = InkWell(
17781812
mouseCursor: effectiveMouseCursor,
@@ -1789,6 +1823,7 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
17891823

17901824
final bool childHasButtonSemantic =
17911825
hintIndex != null || (_selectedIndex != null && widget.selectedItemBuilder == null);
1826+
17921827
return Semantics(
17931828
button: !childHasButtonSemantic,
17941829
expanded: _isMenuExpanded,
@@ -1862,6 +1897,7 @@ class DropdownButtonFormField<T> extends FormField<T> {
18621897
this.barrierDismissible = true,
18631898
this.mouseCursor,
18641899
this.dropdownMenuItemMouseCursor,
1900+
bool isVerticallyExpanded = false,
18651901
// When adding new arguments, consider adding similar arguments to
18661902
// DropdownButton.
18671903
}) : assert(
@@ -1958,6 +1994,7 @@ class DropdownButtonFormField<T> extends FormField<T> {
19581994
barrierDismissible: barrierDismissible,
19591995
mouseCursor: mouseCursor,
19601996
dropdownMenuItemMouseCursor: dropdownMenuItemMouseCursor,
1997+
isVerticallyExpanded: isVerticallyExpanded,
19611998
),
19621999
),
19632000
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fix DropdownButtonFormField underline alignment at bottom.
3+
version: patch

‎packages/material_ui/test/dropdown_form_field_test.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ void main() {
10561056
expect(find.text(currentValue), findsOneWidget);
10571057

10581058
// Tap the DropdownButtonFormField widget
1059-
await tester.tap(find.byType(DropdownButton<String>));
1059+
await tester.tap(find.text(currentValue));
10601060
await tester.pumpAndSettle();
10611061

10621062
// Tap the first dropdown menu item.

‎packages/material_ui/test/dropdown_test.dart‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4966,6 +4966,41 @@ void main() {
49664966
);
49674967
});
49684968

4969+
testWidgets('DropdownButtonFormField underline is at the bottom of the expanded height', (
4970+
WidgetTester tester,
4971+
) async {
4972+
const containerHeight = 200.0;
4973+
4974+
await tester.pumpWidget(
4975+
MaterialApp(
4976+
home: Scaffold(
4977+
body: SizedBox(
4978+
height: containerHeight,
4979+
child: DropdownButtonFormField<int>(
4980+
isExpanded: true,
4981+
isVerticallyExpanded: true,
4982+
items: const [DropdownMenuItem(value: 1, child: Text('Option 1'))],
4983+
onChanged: (_) {},
4984+
),
4985+
),
4986+
),
4987+
),
4988+
);
4989+
4990+
final Finder decorator = find.byType(InputDecorator);
4991+
4992+
final RenderBox box = tester.renderObject(decorator);
4993+
final double width = box.size.width;
4994+
4995+
expect(
4996+
decorator,
4997+
paints..line(
4998+
p1: const Offset(0, containerHeight - 0.5),
4999+
p2: Offset(width, containerHeight - 0.5),
5000+
),
5001+
);
5002+
});
5003+
49695004
testWidgets(
49705005
'DropdownButtonFormField asserts when both errorBuilder and decoration.errorText are provided',
49715006
(WidgetTester tester) async {

0 commit comments

Comments
 (0)