diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 6deba5033fe..7a161c23c00 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -188,6 +188,7 @@ class DropdownMenu extends StatefulWidget { required this.dropdownMenuEntries, this.inputFormatters, this.closeBehavior = DropdownMenuCloseBehavior.all, + this.maxLines = 1, }) : assert(filterCallback == null || enableFilter); /// Determine if the [DropdownMenu] is enabled. @@ -502,6 +503,26 @@ class DropdownMenu extends StatefulWidget { /// Defaults to [DropdownMenuCloseBehavior.all]. final DropdownMenuCloseBehavior closeBehavior; + /// Specifies the maximum number of lines the selected value can display + /// in the [DropdownMenu]. + /// + /// If the provided value is 1, then the text will not wrap, but will scroll + /// horizontally instead. Defaults to 1. + /// + /// If this is null, there is no limit to the number of lines, and the text + /// container will start with enough vertical space for one line and + /// automatically grow to accommodate additional lines as they are entered, up + /// to the height of its constraints. + /// + /// If this is not null, the provided value must be greater than zero. The text + /// field will restrict the input to the given number of lines and take up enough + /// horizontal space to accommodate that number of lines. + /// + /// See also: + /// * [TextField.maxLines], which specifies the maximum number of lines + /// the [TextField] can display. + final int? maxLines; + @override State> createState() => _DropdownMenuState(); } @@ -1005,6 +1026,7 @@ class _DropdownMenuState extends State> { keyboardType: widget.keyboardType, textAlign: widget.textAlign, textAlignVertical: TextAlignVertical.center, + maxLines: widget.maxLines, style: effectiveTextStyle, controller: _localTextEditingController, onEditingComplete: _handleEditingComplete, diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index 41a253d07eb..1101ba425ba 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -3901,6 +3901,35 @@ void main() { }, variant: TargetPlatformVariant.all(), ); + + testWidgets('DropdownMenu passes maxLines to TextField', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp(home: Scaffold(body: DropdownMenu(dropdownMenuEntries: menuChildren))), + ); + TextField textField = tester.widget(find.byType(TextField)); + // Default behavior. + expect(textField.maxLines, 1); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: DropdownMenu(dropdownMenuEntries: menuChildren, maxLines: null), + ), + ), + ); + textField = tester.widget(find.byType(TextField)); + expect(textField.maxLines, null); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: DropdownMenu(dropdownMenuEntries: menuChildren, maxLines: 2), + ), + ), + ); + textField = tester.widget(find.byType(TextField)); + expect(textField.maxLines, 2); + }); } enum TestMenu {