diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 5d9d5984cc5..6a452d99ef5 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -164,6 +164,7 @@ class DropdownMenu extends StatefulWidget { this.menuHeight, this.leadingIcon, this.trailingIcon, + this.showTrailingIcon = true, this.label, this.hintText, this.helperText, @@ -225,8 +226,18 @@ class DropdownMenu extends StatefulWidget { /// An optional icon at the end of the text field. /// /// Defaults to an [Icon] with [Icons.arrow_drop_down]. + /// + /// If [showTrailingIcon] is false, the trailing icon will not be shown. final Widget? trailingIcon; + /// Specifies if the [DropdownMenu] should show a [trailingIcon]. + /// + /// If [trailingIcon] is set, [DropdownMenu] will use that trailing icon, + /// otherwise a default trailing icon will be created. + /// + /// Defaults to true. + final bool showTrailingIcon; + /// Optional widget that describes the input field. /// /// When the input field is empty and unfocused, the label is displayed on @@ -1008,22 +1019,25 @@ class _DropdownMenuState extends State> { builder: (BuildContext context, MenuController controller, Widget? child) { assert(_initialMenu != null); final bool isCollapsed = widget.inputDecorationTheme?.isCollapsed ?? false; - final Widget trailingButton = Padding( - padding: isCollapsed ? EdgeInsets.zero : const EdgeInsets.all(4.0), - child: IconButton( - isSelected: controller.isOpen, - constraints: widget.inputDecorationTheme?.suffixIconConstraints, - padding: isCollapsed ? EdgeInsets.zero : null, - icon: widget.trailingIcon ?? const Icon(Icons.arrow_drop_down), - selectedIcon: widget.selectedTrailingIcon ?? const Icon(Icons.arrow_drop_up), - onPressed: - !widget.enabled - ? null - : () { - handlePressed(controller); - }, - ), - ); + final Widget trailingButton = + widget.showTrailingIcon + ? Padding( + padding: isCollapsed ? EdgeInsets.zero : const EdgeInsets.all(4.0), + child: IconButton( + isSelected: controller.isOpen, + constraints: widget.inputDecorationTheme?.suffixIconConstraints, + padding: isCollapsed ? EdgeInsets.zero : null, + icon: widget.trailingIcon ?? const Icon(Icons.arrow_drop_down), + selectedIcon: widget.selectedTrailingIcon ?? const Icon(Icons.arrow_drop_up), + onPressed: + !widget.enabled + ? null + : () { + handlePressed(controller); + }, + ), + ) + : const SizedBox.shrink(); final Widget leadingButton = Padding( padding: const EdgeInsets.all(8.0), @@ -1070,7 +1084,7 @@ class _DropdownMenuState extends State> { widget.leadingIcon != null ? SizedBox(key: _leadingKey, child: widget.leadingIcon) : null, - suffixIcon: trailingButton, + suffixIcon: widget.showTrailingIcon ? trailingButton : null, ).applyDefaults(effectiveInputDecorationTheme), restorationId: widget.restorationId, ); diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index db8e56ee663..df13d50a8cc 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -4238,6 +4238,47 @@ void main() { final TextField textField = tester.firstWidget(find.byType(TextField)); expect(textField.restorationId, restorationId); }); + + testWidgets( + 'DropdownMenu does not include the default trailing icon when showTrailingIcon is false', + (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: DropdownMenu( + showTrailingIcon: false, + dropdownMenuEntries: menuChildren, + ), + ), + ), + ); + await tester.pump(); + + final Finder iconButton = find.widgetWithIcon(IconButton, Icons.arrow_drop_down); + expect(iconButton, findsNothing); + }, + ); + + testWidgets( + 'DropdownMenu does not include the provided trailing icon when showTrailingIcon is false', + (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: DropdownMenu( + trailingIcon: const Icon(Icons.ac_unit), + showTrailingIcon: false, + dropdownMenuEntries: menuChildren, + ), + ), + ), + ); + await tester.pump(); + + final Finder iconButton = find.widgetWithIcon(IconButton, Icons.ac_unit); + expect(iconButton, findsNothing); + }, + ); } enum TestMenu {