fix MenuItemButton if child is null (#147485)

Fix https://github.com/flutter/flutter/issues/147479

Pre-launch Checklist
This commit is contained in:
zhengzeqin 2024-05-07 10:39:09 +08:00 committed by GitHub
parent 8a7c18c12a
commit 23ae246a2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 16 deletions

View File

@ -856,7 +856,7 @@ class MenuItemButton extends StatefulWidget {
this.trailingIcon, this.trailingIcon,
this.closeOnActivate = true, this.closeOnActivate = true,
this.overflowAxis = Axis.horizontal, this.overflowAxis = Axis.horizontal,
required this.child, this.child,
}); });
/// Called when the button is tapped or otherwise activated. /// Called when the button is tapped or otherwise activated.
@ -1141,7 +1141,7 @@ class _MenuItemButtonState extends State<MenuItemButton> {
trailingIcon: widget.trailingIcon, trailingIcon: widget.trailingIcon,
hasSubmenu: false, hasSubmenu: false,
overflowAxis: _anchor?._orientation ?? widget.overflowAxis, overflowAxis: _anchor?._orientation ?? widget.overflowAxis,
child: widget.child!, child: widget.child,
), ),
); );
@ -1986,7 +1986,7 @@ class _SubmenuButtonState extends State<SubmenuButton> {
trailingIcon: widget.trailingIcon, trailingIcon: widget.trailingIcon,
hasSubmenu: true, hasSubmenu: true,
showDecoration: (controller._anchor!._parent?._orientation ?? Axis.horizontal) == Axis.vertical, showDecoration: (controller._anchor!._parent?._orientation ?? Axis.horizontal) == Axis.vertical,
child: child ?? const SizedBox(), child: child,
), ),
), ),
), ),
@ -2981,7 +2981,7 @@ class _MenuItemLabel extends StatelessWidget {
this.shortcut, this.shortcut,
this.semanticsLabel, this.semanticsLabel,
this.overflowAxis = Axis.vertical, this.overflowAxis = Axis.vertical,
required this.child, this.child,
}); });
/// Whether or not this menu has a submenu. /// Whether or not this menu has a submenu.
@ -3011,8 +3011,8 @@ class _MenuItemLabel extends StatelessWidget {
/// The direction in which the menu item expands. /// The direction in which the menu item expands.
final Axis overflowAxis; final Axis overflowAxis;
/// The required label child widget. /// An optional child widget that is displayed in the label.
final Widget child; final Widget? child;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -3029,14 +3029,15 @@ class _MenuItemLabel extends StatelessWidget {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: <Widget>[ children: <Widget>[
if (leadingIcon != null) leadingIcon!, if (leadingIcon != null) leadingIcon!,
Expanded( if (child != null)
child: ClipRect( Expanded(
child: Padding( child: ClipRect(
padding: leadingIcon != null ? EdgeInsetsDirectional.only(start: horizontalPadding) : EdgeInsets.zero, child: Padding(
child: child, padding: leadingIcon != null ? EdgeInsetsDirectional.only(start: horizontalPadding) : EdgeInsets.zero,
child: child,
),
), ),
), ),
),
], ],
), ),
), ),
@ -3046,10 +3047,11 @@ class _MenuItemLabel extends StatelessWidget {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: <Widget>[ children: <Widget>[
if (leadingIcon != null) leadingIcon!, if (leadingIcon != null) leadingIcon!,
Padding( if (child != null)
padding: leadingIcon != null ? EdgeInsetsDirectional.only(start: horizontalPadding) : EdgeInsets.zero, Padding(
child: child, padding: leadingIcon != null ? EdgeInsetsDirectional.only(start: horizontalPadding) : EdgeInsets.zero,
), child: child,
),
], ],
); );
} }

View File

@ -2532,6 +2532,22 @@ void main() {
..rect(color: overlayColor.withOpacity(0.1)), ..rect(color: overlayColor.withOpacity(0.1)),
); );
}); });
testWidgets('MenuItemButton can build when its child is null', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: SizedBox(
width: 200,
child: MenuItemButton(),
),
),
),
);
// exception `Null check operator used on a null value` would be thrown.
expect(tester.takeException(), isNull);
});
}); });
group('Layout', () { group('Layout', () {