Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Revert "Wire up MenuAnchor, MenuBar, MenuItem-related widgets to aria roles (#165596)" #168339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Expand Down Expand Up @@ -128,7 +126,6 @@ class CustomMenu extends StatelessWidget {
child: Semantics(
scopesRoute: true,
explicitChildNodes: true,
role: SemanticsRole.menu,
child: TapRegion(
groupId: info.tapRegionGroupId,
onTapOutside: (PointerDownEvent event) {
Expand Down
116 changes: 55 additions & 61 deletions examples/api/lib/widgets/raw_menu_anchor/raw_menu_anchor.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Expand Down Expand Up @@ -93,70 +91,66 @@ class _RawMenuAnchorGroupExampleState extends State<RawMenuAnchorGroupExample> {
clipBehavior: Clip.hardEdge,
child: RawMenuAnchorGroup(
controller: controller,
child: Semantics(
role: SemanticsRole.menu,
child: Row(
children: <Widget>[
for (int i = 0; i < menuItems.length; i++)
CustomSubmenu(
focusNode: focusNodes[i],
anchor: Builder(
builder: (BuildContext context) {
final MenuController submenuController =
MenuController.maybeOf(context)!;
final MenuItem item = menuItems[i];
final ButtonStyle openBackground = MenuItemButton.styleFrom(
backgroundColor: const Color(0x0D1A1A1A),
);
return MergeSemantics(
child: Semantics(
expanded: controller.isOpen,
child: MenuItemButton(
style: submenuController.isOpen ? openBackground : null,
onHover: (bool value) {
// If any submenu in the menu bar is already open, other
// submenus should open on hover. Otherwise, blur the menu item
// button if the menu button is no longer hovered.
if (controller.isOpen) {
if (value) {
submenuController.open();
}
} else if (!value) {
Focus.of(context).unfocus();
}
},
onPressed: () {
if (submenuController.isOpen) {
submenuController.close();
} else {
child: Row(
children: <Widget>[
for (int i = 0; i < menuItems.length; i++)
CustomSubmenu(
focusNode: focusNodes[i],
anchor: Builder(
builder: (BuildContext context) {
final MenuController submenuController = MenuController.maybeOf(context)!;
final MenuItem item = menuItems[i];
final ButtonStyle openBackground = MenuItemButton.styleFrom(
backgroundColor: const Color(0x0D1A1A1A),
);
return MergeSemantics(
child: Semantics(
expanded: controller.isOpen,
child: MenuItemButton(
style: submenuController.isOpen ? openBackground : null,
onHover: (bool value) {
// If any submenu in the menu bar is already open, other
// submenus should open on hover. Otherwise, blur the menu item
// button if the menu button is no longer hovered.
if (controller.isOpen) {
if (value) {
submenuController.open();
}
},
leadingIcon: item.leading,
child: Text(item.label),
),
} else if (!value) {
Focus.of(context).unfocus();
}
},
onPressed: () {
if (submenuController.isOpen) {
submenuController.close();
} else {
submenuController.open();
}
},
leadingIcon: item.leading,
child: Text(item.label),
),
);
},
),
children: <Widget>[
for (final MenuItem child in menuItems[i].children ?? <MenuItem>[])
MenuItemButton(
onPressed: () {
setState(() {
_selected = child;
});

// Close the menu bar after a selection.
controller.close();
},
leadingIcon: child.leading,
child: Text(child.label),
),
],
);
},
),
],
),
children: <Widget>[
for (final MenuItem child in menuItems[i].children ?? <MenuItem>[])
MenuItemButton(
onPressed: () {
setState(() {
_selected = child;
});

// Close the menu bar after a selection.
controller.close();
},
leadingIcon: child.leading,
child: Text(child.label),
),
],
),
],
),
),
),
Expand Down
195 changes: 85 additions & 110 deletions packages/flutter/lib/src/material/menu_anchor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -940,9 +940,7 @@ class _MenuItemButtonState extends State<MenuItemButton> {
child = MouseRegion(onHover: _handlePointerHover, onExit: _handlePointerExit, child: child);
}

return MergeSemantics(
child: Semantics(role: SemanticsRole.menuItem, enabled: widget.enabled, child: child),
);
return MergeSemantics(child: child);
}

void _handleFocusChange() {
Expand Down Expand Up @@ -1156,54 +1154,44 @@ class CheckboxMenuButton extends StatelessWidget {

@override
Widget build(BuildContext context) {
return MergeSemantics(
child: Semantics(
role: SemanticsRole.menuItemCheckbox,
checked: value ?? false,
mixed: tristate ? value == null : null,
child: MenuItemButton(
key: key,
onPressed:
onChanged == null
? null
: () {
switch (value) {
case false:
onChanged!(true);
case true:
onChanged!(tristate ? null : false);
case null:
onChanged!(false);
}
},
onHover: onHover,
onFocusChange: onFocusChange,
focusNode: focusNode,
style: style,
shortcut: shortcut,
statesController: statesController,
leadingIcon: ExcludeFocus(
child: IgnorePointer(
child: ConstrainedBox(
constraints: const BoxConstraints(
maxHeight: Checkbox.width,
maxWidth: Checkbox.width,
),
child: Checkbox(
tristate: tristate,
value: value,
onChanged: onChanged,
isError: isError,
),
),
return MenuItemButton(
key: key,
onPressed:
onChanged == null
? null
: () {
switch (value) {
case false:
onChanged!(true);
case true:
onChanged!(tristate ? null : false);
case null:
onChanged!(false);
}
},
onHover: onHover,
onFocusChange: onFocusChange,
focusNode: focusNode,
style: style,
shortcut: shortcut,
statesController: statesController,
leadingIcon: ExcludeFocus(
child: IgnorePointer(
child: ConstrainedBox(
constraints: const BoxConstraints(maxHeight: Checkbox.width, maxWidth: Checkbox.width),
child: Checkbox(
tristate: tristate,
value: value,
onChanged: onChanged,
isError: isError,
),
),
clipBehavior: clipBehavior,
trailingIcon: trailingIcon,
closeOnActivate: closeOnActivate,
child: child,
),
),
clipBehavior: clipBehavior,
trailingIcon: trailingIcon,
closeOnActivate: closeOnActivate,
child: child,
);
}
}
Expand Down Expand Up @@ -1365,49 +1353,40 @@ class RadioMenuButton<T> extends StatelessWidget {

@override
Widget build(BuildContext context) {
return MergeSemantics(
child: Semantics(
role: SemanticsRole.menuItemRadio,
checked: value == groupValue,
child: MenuItemButton(
key: key,
onPressed:
onChanged == null
? null
: () {
if (toggleable && groupValue == value) {
return onChanged!(null);
}
onChanged!(value);
},
onHover: onHover,
onFocusChange: onFocusChange,
focusNode: focusNode,
style: style,
shortcut: shortcut,
statesController: statesController,
leadingIcon: ExcludeFocus(
child: IgnorePointer(
child: ConstrainedBox(
constraints: const BoxConstraints(
maxHeight: Checkbox.width,
maxWidth: Checkbox.width,
),
child: Radio<T>(
value: value,
groupValue: groupValue,
onChanged: onChanged,
toggleable: toggleable,
),
),
return MenuItemButton(
key: key,
onPressed:
onChanged == null
? null
: () {
if (toggleable && groupValue == value) {
return onChanged!(null);
}
onChanged!(value);
},
onHover: onHover,
onFocusChange: onFocusChange,
focusNode: focusNode,
style: style,
shortcut: shortcut,
statesController: statesController,
leadingIcon: ExcludeFocus(
child: IgnorePointer(
child: ConstrainedBox(
constraints: const BoxConstraints(maxHeight: Checkbox.width, maxWidth: Checkbox.width),
child: Radio<T>(
value: value,
groupValue: groupValue,
onChanged: onChanged,
toggleable: toggleable,
),
),
clipBehavior: clipBehavior,
trailingIcon: trailingIcon,
closeOnActivate: closeOnActivate,
child: child,
),
),
clipBehavior: clipBehavior,
trailingIcon: trailingIcon,
closeOnActivate: closeOnActivate,
child: child,
);
}
}
Expand Down Expand Up @@ -1846,24 +1825,23 @@ class _SubmenuButtonState extends State<SubmenuButton> {
}
}

child = Semantics(
container: true,
role: SemanticsRole.menuItem,
expanded: _enabled && controller.isOpen,
enabled: _enabled,
child: TextButton(
style: mergedStyle,
focusNode: _buttonFocusNode,
onFocusChange: _enabled ? widget.onFocusChange : null,
onPressed: _enabled ? toggleShowMenu : null,
isSemanticButton: null,
child: _MenuItemLabel(
leadingIcon: widget.leadingIcon,
trailingIcon: widget.trailingIcon,
hasSubmenu: true,
showDecoration: (_parent?._orientation ?? Axis.horizontal) == Axis.vertical,
submenuIcon: submenuIcon,
child: child,
child = MergeSemantics(
child: Semantics(
expanded: _enabled && controller.isOpen,
child: TextButton(
style: mergedStyle,
focusNode: _buttonFocusNode,
onFocusChange: _enabled ? widget.onFocusChange : null,
onPressed: _enabled ? toggleShowMenu : null,
isSemanticButton: null,
child: _MenuItemLabel(
leadingIcon: widget.leadingIcon,
trailingIcon: widget.trailingIcon,
hasSubmenu: true,
showDecoration: (_parent?._orientation ?? Axis.horizontal) == Axis.vertical,
submenuIcon: submenuIcon,
child: child,
),
),
),
);
Expand Down Expand Up @@ -1894,9 +1872,9 @@ class _SubmenuButtonState extends State<SubmenuButton> {
// After closing the children of this submenu, this submenu button will
// regain focus. Because submenu buttons open on focus, this submenu will
// immediately reopen. To prevent this from happening, we prevent focus on
// SubmenuButtons that do not already have focus using the _isOpenOnFocusEnabled
// SubmenuButtons that do not already have focus using the _openOnFocus
// flag. This flag is reset after one frame.
if (!_buttonFocusNode.hasPrimaryFocus) {
if (!_buttonFocusNode.hasFocus) {
_isOpenOnFocusEnabled = false;
SchedulerBinding.instance.addPostFrameCallback((Duration timestamp) {
FocusManager.instance.applyFocusChangesIfNeeded();
Expand Down Expand Up @@ -3283,10 +3261,7 @@ class _MenuPanelState extends State<_MenuPanel> {
);
}

return Semantics(
role: widget.orientation == Axis.vertical ? SemanticsRole.menu : SemanticsRole.menuBar,
child: ConstrainedBox(constraints: effectiveConstraints, child: menuPanel),
);
return ConstrainedBox(constraints: effectiveConstraints, child: menuPanel);
}

Widget _intrinsicCrossSize({required Widget child}) {
Expand Down
Loading
Loading