diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index f020070801..15d811d862 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -617,8 +617,13 @@ typedef List> PopupMenuItemBuilder(BuildContext context); /// Displays a menu when pressed and calls [onSelected] when the menu is dismissed /// because an item was selected. The value passed to [onSelected] is the value of -/// the selected menu item. If child is null then a standard 'navigation/more_vert' -/// icon is created. +/// the selected menu item. +/// +/// One of [child] or [icon] may be provided, but not both. If [icon] is provided, +/// then [PopupMenuButton] behaves like an [IconButton]. +/// +/// If both are null, then a standard overflow icon is created (depending on the +/// platform). /// /// ## Sample code /// @@ -672,8 +677,10 @@ class PopupMenuButton extends StatefulWidget { this.tooltip: 'Show menu', this.elevation: 8.0, this.padding: const EdgeInsets.all(8.0), - this.child + this.child, + this.icon, }) : assert(itemBuilder != null), + assert(!(child != null && icon != null)), // fails if passed both parameters super(key: key); /// Called when the button is pressed to create the items to show in the menu. @@ -702,9 +709,12 @@ class PopupMenuButton extends StatefulWidget { /// to set the padding to zero. final EdgeInsets padding; - /// The widget below this widget in the tree. + /// If provided, the widget used for this button. final Widget child; + /// If provided, the icon used for this button. + final Icon icon; + @override _PopupMenuButtonState createState() => new _PopupMenuButtonState(); } @@ -747,17 +757,16 @@ class _PopupMenuButtonState extends State> { @override Widget build(BuildContext context) { - if (widget.child == null) { - return new IconButton( - icon: _getIcon(Theme.of(context).platform), - padding: widget.padding, - tooltip: widget.tooltip, - onPressed: showButtonMenu, - ); - } - return new InkWell( - onTap: showButtonMenu, - child: widget.child, - ); + return widget.child != null + ? new InkWell( + onTap: showButtonMenu, + child: widget.child, + ) + : new IconButton( + icon: widget.icon ?? _getIcon(Theme.of(context).platform), + padding: widget.padding, + tooltip: widget.tooltip, + onPressed: showButtonMenu, + ); } } diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index e1bb007961..8b6f03dad8 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -90,4 +90,46 @@ void main() { expect(find.byIcon(Icons.more_vert), findsNothing); expect(find.byIcon(Icons.more_horiz), findsOneWidget); }); + + group('PopupMenuButton with Icon', () { + + // Helper function to create simple and valid popup menus. + List> simplePopupMenuItemBuilder(BuildContext context) { + return >[ + const PopupMenuItem( + value: 1, + child: const Text('1'), + ), + ]; + } + + testWidgets('PopupMenuButton fails when given both child and icon', (WidgetTester tester) async { + expect(() { + new PopupMenuButton( + child: const Text('heyo'), + icon: const Icon(Icons.view_carousel), + itemBuilder: simplePopupMenuItemBuilder, + ); + }, throwsA(new isInstanceOf())); + }); + + testWidgets('PopupMenuButton creates IconButton when given an icon', (WidgetTester tester) async { + final PopupMenuButton button = new PopupMenuButton( + icon: const Icon(Icons.view_carousel), + itemBuilder: simplePopupMenuItemBuilder, + ); + + await tester.pumpWidget(new MaterialApp( + home: new Scaffold( + appBar: new AppBar( + actions: [button], + ), + ), + ), + ); + + expect(find.byType(IconButton), findsOneWidget); + expect(find.byIcon(Icons.view_carousel), findsOneWidget); + }); + }); }