@@ -599,15 +599,7 @@ class _PopupMenu<T> extends StatelessWidget {
599599
600600// Positioning of the menu on the screen.
601601class _PopupMenuRouteLayout extends SingleChildLayoutDelegate {
602- _PopupMenuRouteLayout (
603- this .position,
604- this .itemSizes,
605- this .selectedItemIndex,
606- this .textDirection,
607- this .topPadding,
608- this .bottomPadding,
609- this .placement,
610- );
602+ _PopupMenuRouteLayout (this .position, this .itemSizes, this .selectedItemIndex, this .textDirection);
611603
612604 // Rectangle of underlying button, relative to the overlay's dimensions.
613605 final RelativeRect position;
@@ -623,15 +615,6 @@ class _PopupMenuRouteLayout extends SingleChildLayoutDelegate {
623615 // Whether to prefer going to the left or to the right.
624616 final TextDirection textDirection;
625617
626- // Top padding of unsafe area.
627- final double topPadding;
628-
629- // Bottom padding of unsafe area.
630- final double bottomPadding;
631-
632- // The placement of the menu.
633- final PopupMenuPlacement placement;
634-
635618 // We put the child wherever position specifies, so long as it will fit within
636619 // the specified parent size padded (inset) by 8. If necessary, we adjust the
637620 // child's position so that it fits.
@@ -641,8 +624,7 @@ class _PopupMenuRouteLayout extends SingleChildLayoutDelegate {
641624 // The menu can be at most the size of the overlay minus 8.0 pixels in each
642625 // direction.
643626 return BoxConstraints .loose (
644- constraints.biggest - Offset (_kMenuScreenPadding * 2.0 ,
645- _kMenuScreenPadding * 2.0 + topPadding + bottomPadding) as Size ,
627+ constraints.biggest - const Offset (_kMenuScreenPadding * 2.0 , _kMenuScreenPadding * 2.0 ) as Size ,
646628 );
647629 }
648630
@@ -652,23 +634,14 @@ class _PopupMenuRouteLayout extends SingleChildLayoutDelegate {
652634 // childSize: The size of the menu, when fully open, as determined by
653635 // getConstraintsForChild.
654636
655- final double buttonHeight = size.height - position.top - position.bottom;
656-
657637 // Find the ideal vertical position.
658- // Default vertical position is below the element that generates it.
659- double y = placement == PopupMenuPlacement .belowButton
660- ? position.top + buttonHeight
661- : position.top;
638+ double y = position.top;
662639 if (selectedItemIndex != null && itemSizes != null ) {
663640 double selectedItemOffset = _kMenuVerticalPadding;
664641 for (int index = 0 ; index < selectedItemIndex! ; index += 1 )
665642 selectedItemOffset += itemSizes[index]! .height;
666643 selectedItemOffset += itemSizes[selectedItemIndex! ]! .height / 2 ;
667- if (placement == PopupMenuPlacement .belowButton) {
668- y = y - buttonHeight / 2.0 - selectedItemOffset;
669- } else {
670- y = y + buttonHeight / 2.0 - selectedItemOffset;
671- }
644+ y = position.top + (size.height - position.top - position.bottom) / 2.0 - selectedItemOffset;
672645 }
673646
674647 // Find the ideal horizontal position.
@@ -698,10 +671,10 @@ class _PopupMenuRouteLayout extends SingleChildLayoutDelegate {
698671 x = _kMenuScreenPadding;
699672 else if (x + childSize.width > size.width - _kMenuScreenPadding)
700673 x = size.width - childSize.width - _kMenuScreenPadding;
701- if (y < _kMenuScreenPadding + topPadding )
702- y = _kMenuScreenPadding + topPadding ;
674+ if (y < _kMenuScreenPadding)
675+ y = _kMenuScreenPadding;
703676 else if (y + childSize.height > size.height - _kMenuScreenPadding)
704- y = size.height - bottomPadding - _kMenuScreenPadding - childSize.height ;
677+ y = size.height - childSize.height - _kMenuScreenPadding ;
705678 return Offset (x, y);
706679 }
707680
@@ -713,9 +686,9 @@ class _PopupMenuRouteLayout extends SingleChildLayoutDelegate {
713686 assert (itemSizes.length == oldDelegate.itemSizes.length);
714687
715688 return position != oldDelegate.position
716- || selectedItemIndex != oldDelegate.selectedItemIndex
717- || textDirection != oldDelegate.textDirection
718- || ! listEquals (itemSizes, oldDelegate.itemSizes);
689+ || selectedItemIndex != oldDelegate.selectedItemIndex
690+ || textDirection != oldDelegate.textDirection
691+ || ! listEquals (itemSizes, oldDelegate.itemSizes);
719692 }
720693}
721694
@@ -730,7 +703,6 @@ class _PopupMenuRoute<T> extends PopupRoute<T> {
730703 this .shape,
731704 this .color,
732705 required this .capturedThemes,
733- required this .placement,
734706 }) : itemSizes = List <Size ?>.filled (items.length, null );
735707
736708 final RelativeRect position;
@@ -742,7 +714,6 @@ class _PopupMenuRoute<T> extends PopupRoute<T> {
742714 final ShapeBorder ? shape;
743715 final Color ? color;
744716 final CapturedThemes capturedThemes;
745- final PopupMenuPlacement placement;
746717
747718 @override
748719 Animation <double > createAnimation () {
@@ -778,22 +749,20 @@ class _PopupMenuRoute<T> extends PopupRoute<T> {
778749
779750 final Widget menu = _PopupMenu <T >(route: this , semanticLabel: semanticLabel);
780751
781- return Builder (
782- builder: (BuildContext context) {
783- final MediaQueryData mediaQuery = MediaQuery .of (context);
784- return CustomSingleChildLayout (
785- delegate: _PopupMenuRouteLayout (
786- position,
787- itemSizes,
788- selectedItemIndex,
789- Directionality .of (context),
790- mediaQuery.padding.top,
791- mediaQuery.padding.bottom,
792- placement,
793- ),
794- child: capturedThemes.wrap (menu),
795- );
796- },
752+ return SafeArea (
753+ child: Builder (
754+ builder: (BuildContext context) {
755+ return CustomSingleChildLayout (
756+ delegate: _PopupMenuRouteLayout (
757+ position,
758+ itemSizes,
759+ selectedItemIndex,
760+ Directionality .of (context),
761+ ),
762+ child: capturedThemes.wrap (menu),
763+ );
764+ },
765+ ),
797766 );
798767 }
799768}
@@ -863,7 +832,6 @@ Future<T?> showMenu<T>({
863832 ShapeBorder ? shape,
864833 Color ? color,
865834 bool useRootNavigator = false ,
866- PopupMenuPlacement placement = PopupMenuPlacement .aboveButton,
867835}) {
868836 assert (context != null );
869837 assert (position != null );
@@ -893,7 +861,6 @@ Future<T?> showMenu<T>({
893861 shape: shape,
894862 color: color,
895863 capturedThemes: InheritedTheme .capture (from: context, to: navigator.context),
896- placement: placement,
897864 ));
898865}
899866
@@ -916,17 +883,6 @@ typedef PopupMenuCanceled = void Function();
916883/// Used by [PopupMenuButton.itemBuilder] .
917884typedef PopupMenuItemBuilder <T > = List <PopupMenuEntry <T >> Function (BuildContext context);
918885
919- /// The placement of the menu popped up by press the [PopupMenuButton] .
920- ///
921- /// Used by [PopupMenuButton.placement] .
922- enum PopupMenuPlacement {
923- /// The popup menu is positioned above the button that generates it.
924- aboveButton,
925-
926- /// The popup menu is positioned below the button that generates it.
927- belowButton,
928- }
929-
930886/// Displays a menu when pressed and calls [onSelected] when the menu is dismissed
931887/// because an item was selected. The value passed to [onSelected] is the value of
932888/// the selected menu item.
@@ -999,7 +955,6 @@ class PopupMenuButton<T> extends StatefulWidget {
999955 this .shape,
1000956 this .color,
1001957 this .enableFeedback,
1002- this .placement = PopupMenuPlacement .aboveButton,
1003958 }) : assert (itemBuilder != null ),
1004959 assert (offset != null ),
1005960 assert (enabled != null ),
@@ -1051,7 +1006,7 @@ class PopupMenuButton<T> extends StatefulWidget {
10511006
10521007 /// The offset applied to the Popup Menu Button.
10531008 ///
1054- /// When not set, the Popup Menu Button will be positioned directly below
1009+ /// When not set, the Popup Menu Button will be positioned directly next to
10551010 /// the button that was used to create it.
10561011 final Offset offset;
10571012
@@ -1094,11 +1049,6 @@ class PopupMenuButton<T> extends StatefulWidget {
10941049 /// * [Feedback] for providing platform-specific feedback to certain actions.
10951050 final bool ? enableFeedback;
10961051
1097- /// The placement of the menu popped up by press the [PopupMenuButton] .
1098- ///
1099- /// Default to [PopupMenuPlacement.aboveButton] .
1100- final PopupMenuPlacement placement;
1101-
11021052 /// If provided, the size of the [Icon] .
11031053 ///
11041054 /// If this property is null, the default size is 24.0 pixels.
@@ -1113,9 +1063,8 @@ class PopupMenuButton<T> extends StatefulWidget {
11131063/// See [showButtonMenu] for a way to programmatically open the popup menu
11141064/// of your button state.
11151065class PopupMenuButtonState <T > extends State <PopupMenuButton <T >> {
1116- final GlobalKey _menuButtonKey = GlobalKey ();
11171066 /// A method to show a popup menu with the items supplied to
1118- /// [PopupMenuButton.itemBuilder] at the position below your [PopupMenuButton] .
1067+ /// [PopupMenuButton.itemBuilder] at the position of your [PopupMenuButton] .
11191068 ///
11201069 /// By default, it is called when the user taps the button and [PopupMenuButton.enabled]
11211070 /// is set to `true` . Moreover, you can open the button by calling the method manually.
@@ -1124,14 +1073,8 @@ class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
11241073 /// show the menu of the button with `globalKey.currentState.showButtonMenu` .
11251074 void showButtonMenu () {
11261075 final PopupMenuThemeData popupMenuTheme = PopupMenuTheme .of (context);
1076+ final RenderBox button = context.findRenderObject ()! as RenderBox ;
11271077 final RenderBox overlay = Navigator .of (context).overlay! .context.findRenderObject ()! as RenderBox ;
1128- final RenderBox button;
1129- if (widget.placement == PopupMenuPlacement .belowButton) {
1130- button = _menuButtonKey.currentContext! .findRenderObject ()! as RenderBox ;
1131- } else {
1132- // Backward compatible.
1133- button = context.findRenderObject ()! as RenderBox ;
1134- }
11351078 final RelativeRect position = RelativeRect .fromRect (
11361079 Rect .fromPoints (
11371080 button.localToGlobal (widget.offset, ancestor: overlay),
@@ -1150,7 +1093,6 @@ class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
11501093 position: position,
11511094 shape: widget.shape ?? popupMenuTheme.shape,
11521095 color: widget.color ?? popupMenuTheme.color,
1153- placement: widget.placement,
11541096 )
11551097 .then <void >((T ? newValue) {
11561098 if (! mounted)
@@ -1190,40 +1132,18 @@ class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
11901132 child: InkWell (
11911133 onTap: widget.enabled ? showButtonMenu : null ,
11921134 canRequestFocus: _canRequestFocus,
1135+ child: widget.child,
11931136 enableFeedback: enableFeedback,
1194- child: LayoutBuilder (
1195- builder: (BuildContext context, BoxConstraints constraints) {
1196- return UnconstrainedBox (
1197- constrainedAxis: Axis .horizontal,
1198- child: LimitedBox (
1199- key: _menuButtonKey,
1200- maxHeight: constraints.maxHeight,
1201- child: widget.child,
1202- ),
1203- );
1204- },
1205- ),
12061137 ),
12071138 );
12081139
1209- return LayoutBuilder (
1210- builder: (BuildContext context, BoxConstraints constraints) {
1211- return UnconstrainedBox (
1212- constrainedAxis: Axis .horizontal,
1213- child: LimitedBox (
1214- maxHeight: constraints.maxHeight,
1215- child: IconButton (
1216- key: _menuButtonKey,
1217- icon: widget.icon ?? Icon (Icons .adaptive.more),
1218- padding: widget.padding,
1219- iconSize: widget.iconSize ?? 24.0 ,
1220- tooltip: widget.tooltip ?? MaterialLocalizations .of (context).showMenuTooltip,
1221- onPressed: widget.enabled ? showButtonMenu : null ,
1222- enableFeedback: enableFeedback,
1223- ),
1224- ),
1225- );
1226- },
1140+ return IconButton (
1141+ icon: widget.icon ?? Icon (Icons .adaptive.more),
1142+ padding: widget.padding,
1143+ iconSize: widget.iconSize ?? 24.0 ,
1144+ tooltip: widget.tooltip ?? MaterialLocalizations .of (context).showMenuTooltip,
1145+ onPressed: widget.enabled ? showButtonMenu : null ,
1146+ enableFeedback: enableFeedback,
12271147 );
12281148 }
12291149}
0 commit comments