From 752147e2b1b592e31a712dfc9d24354b5605909e Mon Sep 17 00:00:00 2001 From: Darren Austin Date: Tue, 14 May 2019 15:48:13 -0700 Subject: [PATCH] Added 'enabled' property to the PopupMenuButton (#32527) Added 'enabled' property to the PopupMenuButton to allow the button to be disabled in the case where the menu would be empty. --- .../flutter/lib/src/material/popup_menu.dart | 62 +++++++++++++------ .../test/material/popup_menu_test.dart | 49 +++++++++++++++ 2 files changed, 91 insertions(+), 20 deletions(-) diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 400e56cde8..8210a2f9ed 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -663,6 +663,8 @@ class _PopupMenuRoute extends PopupRoute { /// Show a popup menu that contains the `items` at `position`. /// +/// `items` should be non-null and not empty. +/// /// If `initialValue` is specified then the first item with a matching value /// will be highlighted and the value of `position` gives the rectangle whose /// vertical center will be aligned with the vertical center of the highlighted @@ -829,8 +831,10 @@ class PopupMenuButton extends StatefulWidget { this.child, this.icon, this.offset = Offset.zero, + this.enabled = true, }) : assert(itemBuilder != null), assert(offset != null), + assert(enabled != null), assert(!(child != null && icon != null)), // fails if passed both parameters super(key: key); @@ -880,6 +884,20 @@ class PopupMenuButton extends StatefulWidget { /// the button that was used to create it. final Offset offset; + /// Whether this popup menu button is interactive. + /// + /// Must be non-null, defaults to `true` + /// + /// If `true` the button will respond to presses by displaying the menu. + /// + /// If `false`, the button is styled with the disabled color from the + /// current [Theme] and will not respond to presses or show the popup + /// menu and [onSelected], [onCanceled] and [itemBuilder] will not be called. + /// + /// This can be useful in situations where the app needs to show the button, + /// but doesn't currently have anything to show in the menu. + final bool enabled; + @override _PopupMenuButtonState createState() => _PopupMenuButtonState(); } @@ -895,24 +913,28 @@ class _PopupMenuButtonState extends State> { ), Offset.zero & overlay.size, ); - showMenu( - context: context, - elevation: widget.elevation, - items: widget.itemBuilder(context), - initialValue: widget.initialValue, - position: position, - ) - .then((T newValue) { - if (!mounted) - return null; - if (newValue == null) { - if (widget.onCanceled != null) - widget.onCanceled(); - return null; - } - if (widget.onSelected != null) - widget.onSelected(newValue); - }); + final List> items = widget.itemBuilder(context); + // Only show the menu if there is something to show + if (items.isNotEmpty) { + showMenu( + context: context, + elevation: widget.elevation, + items: items, + initialValue: widget.initialValue, + position: position, + ) + .then((T newValue) { + if (!mounted) + return null; + if (newValue == null) { + if (widget.onCanceled != null) + widget.onCanceled(); + return null; + } + if (widget.onSelected != null) + widget.onSelected(newValue); + }); + } } Icon _getIcon(TargetPlatform platform) { @@ -932,14 +954,14 @@ class _PopupMenuButtonState extends State> { assert(debugCheckHasMaterialLocalizations(context)); return widget.child != null ? InkWell( - onTap: showButtonMenu, + onTap: widget.enabled ? showButtonMenu : null, child: widget.child, ) : IconButton( icon: widget.icon ?? _getIcon(Theme.of(context).platform), padding: widget.padding, tooltip: widget.tooltip ?? MaterialLocalizations.of(context).showMenuTooltip, - onPressed: showButtonMenu, + onPressed: widget.enabled ? showButtonMenu : null, ); } } diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index 1ce1761547..e8d53471c1 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -126,6 +126,55 @@ void main() { expect(cancels, equals(2)); }); + testWidgets('disabled PopupMenuButton will not call itemBuilder, onSelected or onCanceled', (WidgetTester tester) async { + final Key popupButtonKey = UniqueKey(); + bool itemBuilderCalled = false; + bool onSelectedCalled = false; + bool onCanceledCalled = false; + + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Column( + children: [ + PopupMenuButton( + key: popupButtonKey, + enabled: false, + itemBuilder: (BuildContext context) { + itemBuilderCalled = true; + return >[ + const PopupMenuItem( + value: 1, + child: Text('Tap me please!'), + ), + ]; + }, + onSelected: (int selected) => onSelectedCalled = true, + onCanceled: () => onCanceledCalled = true, + ), + ], + ), + ), + ), + ); + + // Try to bring up the popup menu and select the first item from it + await tester.tap(find.byKey(popupButtonKey)); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(popupButtonKey)); + await tester.pumpAndSettle(); + expect(itemBuilderCalled, isFalse); + expect(onSelectedCalled, isFalse); + + // Try to bring up the popup menu and tap outside it to cancel the menu + await tester.tap(find.byKey(popupButtonKey)); + await tester.pumpAndSettle(); + await tester.tapAt(const Offset(0.0, 0.0)); + await tester.pumpAndSettle(); + expect(itemBuilderCalled, isFalse); + expect(onCanceledCalled, isFalse); + }); + testWidgets('PopupMenuButton is horizontal on iOS', (WidgetTester tester) async { Widget build(TargetPlatform platform) { return MaterialApp(