diff --git a/packages/flutter/lib/src/material/menu_anchor.dart b/packages/flutter/lib/src/material/menu_anchor.dart index 45f4f240b4..1bb1e2c33d 100644 --- a/packages/flutter/lib/src/material/menu_anchor.dart +++ b/packages/flutter/lib/src/material/menu_anchor.dart @@ -856,6 +856,7 @@ class MenuItemButton extends StatefulWidget { this.requestFocusOnHover = true, this.onFocusChange, this.focusNode, + this.autofocus = false, this.shortcut, this.semanticsLabel, this.style, @@ -897,6 +898,9 @@ class MenuItemButton extends StatefulWidget { /// {@macro flutter.widgets.Focus.focusNode} final FocusNode? focusNode; + /// {@macro flutter.widgets.Focus.autofocus} + final bool autofocus; + /// The optional shortcut that selects this [MenuItemButton]. /// /// {@macro flutter.material.MenuBar.shortcuts_note} @@ -1140,6 +1144,7 @@ class _MenuItemButtonState extends State { onFocusChange: widget.enabled ? widget.onFocusChange : null, focusNode: _focusNode, style: mergedStyle, + autofocus: widget.enabled && widget.autofocus, statesController: widget.statesController, clipBehavior: widget.clipBehavior, isSemanticButton: null, diff --git a/packages/flutter/test/material/menu_anchor_test.dart b/packages/flutter/test/material/menu_anchor_test.dart index 5063d28616..100cdf60e4 100644 --- a/packages/flutter/test/material/menu_anchor_test.dart +++ b/packages/flutter/test/material/menu_anchor_test.dart @@ -2229,6 +2229,45 @@ void main() { expect(find.text('leadingIcon'), findsOneWidget); }); + testWidgets('autofocus is used when set and widget is enabled', + (WidgetTester tester) async { + + listenForFocusChanges(); + + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Column( + children: [ + MenuAnchor( + controller: controller, + menuChildren: [ + MenuItemButton( + autofocus: true, + // Required for clickability. + onPressed: () {}, + child: Text(TestMenu.mainMenu0.label), + ), + MenuItemButton( + onPressed: () {}, + child: Text(TestMenu.mainMenu1.label), + ), + ], + ), + const Expanded(child: Placeholder()), + ], + ), + ), + ), + ); + + controller.open(); + await tester.pump(); + + expect(controller.isOpen, equals(true)); + expect(focusedMenu, equals('MenuItemButton(Text("${TestMenu.mainMenu0.label}"))')); + }); + testWidgets('trailingIcon is used when set', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp(