Implement Material MenuBar and MenuAnchor (#112239)
This implements a MenuBar widget that can render a Material menu bar, and a MenuAnchor widget used to create a cascading menu in a region. The menus are drawn in the overlay, while the menu bar itself is in the regular widget tree. Keyboard traversal works between the two. This implementation of the MenuBar uses MenuAnchor to create a cascading menu that contains widgets representing the menu items. These menu items can be any kind of widget, but are typically SubmenuButtons that host submenus, or MenuItemButtons that have shortcut hints (but don't actually activate the shortcuts) and don't host submenus. Cascading menus can be created outside of a MenuBar by using a MenuAnchor. They can be either given a specific location to appear (a coordinate), or they can be located by the MenuAnchor region that wraps the control that opens them. The developer may also create a MenuController to pass to the various menu primitives (MenuBar or MenuAnchor) to associate menus so that they can be traversed together and closed together. Creating a controller is not required.
This commit is contained in:
212
examples/api/lib/material/menu_anchor/menu_anchor.0.dart
Normal file
212
examples/api/lib/material/menu_anchor/menu_anchor.0.dart
Normal file
@@ -0,0 +1,212 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
/// Flutter code sample for [MenuAnchor].
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
void main() => runApp(const MenuApp());
|
||||
|
||||
/// An enhanced enum to define the available menus and their shortcuts.
|
||||
///
|
||||
/// Using an enum for menu definition is not required, but this illustrates how
|
||||
/// they could be used for simple menu systems.
|
||||
enum MenuEntry {
|
||||
about('About'),
|
||||
showMessage('Show Message', SingleActivator(LogicalKeyboardKey.keyS, control: true)),
|
||||
hideMessage('Hide Message', SingleActivator(LogicalKeyboardKey.keyS, control: true)),
|
||||
colorMenu('Color Menu'),
|
||||
colorRed('Red Background', SingleActivator(LogicalKeyboardKey.keyR, control: true)),
|
||||
colorGreen('Green Background', SingleActivator(LogicalKeyboardKey.keyG, control: true)),
|
||||
colorBlue('Blue Background', SingleActivator(LogicalKeyboardKey.keyB, control: true));
|
||||
|
||||
const MenuEntry(this.label, [this.shortcut]);
|
||||
final String label;
|
||||
final MenuSerializableShortcut? shortcut;
|
||||
}
|
||||
|
||||
class MyCascadingMenu extends StatefulWidget {
|
||||
const MyCascadingMenu({super.key, required this.message});
|
||||
|
||||
final String message;
|
||||
|
||||
@override
|
||||
State<MyCascadingMenu> createState() => _MyCascadingMenuState();
|
||||
}
|
||||
|
||||
class _MyCascadingMenuState extends State<MyCascadingMenu> {
|
||||
MenuEntry? _lastSelection;
|
||||
final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Menu Button');
|
||||
ShortcutRegistryEntry? _shortcutsEntry;
|
||||
|
||||
Color get backgroundColor => _backgroundColor;
|
||||
Color _backgroundColor = Colors.red;
|
||||
set backgroundColor(Color value) {
|
||||
if (_backgroundColor != value) {
|
||||
setState(() {
|
||||
_backgroundColor = value;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
bool get showingMessage => _showingMessage;
|
||||
bool _showingMessage = false;
|
||||
set showingMessage(bool value) {
|
||||
if (_showingMessage != value) {
|
||||
setState(() {
|
||||
_showingMessage = value;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
// Dispose of any previously registered shortcuts, since they are about to
|
||||
// be replaced.
|
||||
_shortcutsEntry?.dispose();
|
||||
// Collect the shortcuts from the different menu selections so that they can
|
||||
// be registered to apply to the entire app. Menus don't register their
|
||||
// shortcuts, they only display the shortcut hint text.
|
||||
final Map<ShortcutActivator, Intent> shortcuts = <ShortcutActivator, Intent>{
|
||||
for (final MenuEntry item in MenuEntry.values)
|
||||
if (item.shortcut != null) item.shortcut!: VoidCallbackIntent(() => _activate(item)),
|
||||
};
|
||||
// Register the shortcuts with the ShortcutRegistry so that they are
|
||||
// available to the entire application.
|
||||
_shortcutsEntry = ShortcutRegistry.of(context).addAll(shortcuts);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_shortcutsEntry?.dispose();
|
||||
_buttonFocusNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
MenuAnchor(
|
||||
childFocusNode: _buttonFocusNode,
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
child: Text(MenuEntry.about.label),
|
||||
onPressed: () => _activate(MenuEntry.about),
|
||||
),
|
||||
if (_showingMessage) MenuItemButton(
|
||||
onPressed: () => _activate(MenuEntry.hideMessage),
|
||||
shortcut: MenuEntry.hideMessage.shortcut,
|
||||
child: Text(MenuEntry.hideMessage.label),
|
||||
),
|
||||
if (!_showingMessage) MenuItemButton(
|
||||
onPressed: () => _activate(MenuEntry.showMessage),
|
||||
shortcut: MenuEntry.showMessage.shortcut,
|
||||
child: Text(MenuEntry.showMessage.label),
|
||||
),
|
||||
SubmenuButton(
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
onPressed: () => _activate(MenuEntry.colorRed),
|
||||
shortcut: MenuEntry.colorRed.shortcut,
|
||||
child: Text(MenuEntry.colorRed.label),
|
||||
),
|
||||
MenuItemButton(
|
||||
onPressed: () => _activate(MenuEntry.colorGreen),
|
||||
shortcut: MenuEntry.colorGreen.shortcut,
|
||||
child: Text(MenuEntry.colorGreen.label),
|
||||
),
|
||||
MenuItemButton(
|
||||
onPressed: () => _activate(MenuEntry.colorBlue),
|
||||
shortcut: MenuEntry.colorBlue.shortcut,
|
||||
child: Text(MenuEntry.colorBlue.label),
|
||||
),
|
||||
],
|
||||
child: const Text('Background Color'),
|
||||
),
|
||||
],
|
||||
builder: (BuildContext context, MenuController controller, Widget? child) {
|
||||
return TextButton(
|
||||
focusNode: _buttonFocusNode,
|
||||
onPressed: () {
|
||||
if (controller.isOpen) {
|
||||
controller.close();
|
||||
} else {
|
||||
controller.open();
|
||||
}
|
||||
},
|
||||
child: const Text('OPEN MENU'),
|
||||
);
|
||||
},
|
||||
),
|
||||
Expanded(
|
||||
child: Container(
|
||||
alignment: Alignment.center,
|
||||
color: backgroundColor,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: Text(
|
||||
showingMessage ? widget.message : '',
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
),
|
||||
Text(_lastSelection != null ? 'Last Selected: ${_lastSelection!.label}' : ''),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _activate(MenuEntry selection) {
|
||||
setState(() {
|
||||
_lastSelection = selection;
|
||||
});
|
||||
|
||||
switch (selection) {
|
||||
case MenuEntry.about:
|
||||
showAboutDialog(
|
||||
context: context,
|
||||
applicationName: 'MenuBar Sample',
|
||||
applicationVersion: '1.0.0',
|
||||
);
|
||||
break;
|
||||
case MenuEntry.hideMessage:
|
||||
case MenuEntry.showMessage:
|
||||
showingMessage = !showingMessage;
|
||||
break;
|
||||
case MenuEntry.colorMenu:
|
||||
break;
|
||||
case MenuEntry.colorRed:
|
||||
backgroundColor = Colors.red;
|
||||
break;
|
||||
case MenuEntry.colorGreen:
|
||||
backgroundColor = Colors.green;
|
||||
break;
|
||||
case MenuEntry.colorBlue:
|
||||
backgroundColor = Colors.blue;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MenuApp extends StatelessWidget {
|
||||
const MenuApp({super.key});
|
||||
|
||||
static const String kMessage = '"Talk less. Smile more." - A. Burr';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: Scaffold(body: MyCascadingMenu(message: kMessage)),
|
||||
);
|
||||
}
|
||||
}
|
||||
211
examples/api/lib/material/menu_anchor/menu_anchor.1.dart
Normal file
211
examples/api/lib/material/menu_anchor/menu_anchor.1.dart
Normal file
@@ -0,0 +1,211 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
/// Flutter code sample for [MenuAnchor].
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
void main() => runApp(const ContextMenuApp());
|
||||
|
||||
/// An enhanced enum to define the available menus and their shortcuts.
|
||||
///
|
||||
/// Using an enum for menu definition is not required, but this illustrates how
|
||||
/// they could be used for simple menu systems.
|
||||
enum MenuEntry {
|
||||
about('About'),
|
||||
showMessage('Show Message', SingleActivator(LogicalKeyboardKey.keyS, control: true)),
|
||||
hideMessage('Hide Message', SingleActivator(LogicalKeyboardKey.keyS, control: true)),
|
||||
colorMenu('Color Menu'),
|
||||
colorRed('Red Background', SingleActivator(LogicalKeyboardKey.keyR, control: true)),
|
||||
colorGreen('Green Background', SingleActivator(LogicalKeyboardKey.keyG, control: true)),
|
||||
colorBlue('Blue Background', SingleActivator(LogicalKeyboardKey.keyB, control: true));
|
||||
|
||||
const MenuEntry(this.label, [this.shortcut]);
|
||||
final String label;
|
||||
final MenuSerializableShortcut? shortcut;
|
||||
}
|
||||
|
||||
class MyContextMenu extends StatefulWidget {
|
||||
const MyContextMenu({super.key, required this.message});
|
||||
|
||||
final String message;
|
||||
|
||||
@override
|
||||
State<MyContextMenu> createState() => _MyContextMenuState();
|
||||
}
|
||||
|
||||
class _MyContextMenuState extends State<MyContextMenu> {
|
||||
MenuEntry? _lastSelection;
|
||||
final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Menu Button');
|
||||
final MenuController _menuController = MenuController();
|
||||
ShortcutRegistryEntry? _shortcutsEntry;
|
||||
|
||||
Color get backgroundColor => _backgroundColor;
|
||||
Color _backgroundColor = Colors.red;
|
||||
set backgroundColor(Color value) {
|
||||
if (_backgroundColor != value) {
|
||||
setState(() {
|
||||
_backgroundColor = value;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
bool get showingMessage => _showingMessage;
|
||||
bool _showingMessage = false;
|
||||
set showingMessage(bool value) {
|
||||
if (_showingMessage != value) {
|
||||
setState(() {
|
||||
_showingMessage = value;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
// Dispose of any previously registered shortcuts, since they are about to
|
||||
// be replaced.
|
||||
_shortcutsEntry?.dispose();
|
||||
// Collect the shortcuts from the different menu selections so that they can
|
||||
// be registered to apply to the entire app. Menus don't register their
|
||||
// shortcuts, they only display the shortcut hint text.
|
||||
final Map<ShortcutActivator, Intent> shortcuts = <ShortcutActivator, Intent>{
|
||||
for (final MenuEntry item in MenuEntry.values)
|
||||
if (item.shortcut != null) item.shortcut!: VoidCallbackIntent(() => _activate(item)),
|
||||
};
|
||||
// Register the shortcuts with the ShortcutRegistry so that they are
|
||||
// available to the entire application.
|
||||
_shortcutsEntry = ShortcutRegistry.of(context).addAll(shortcuts);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_shortcutsEntry?.dispose();
|
||||
_buttonFocusNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(50),
|
||||
child: GestureDetector(
|
||||
onTapDown: _handleTapDown,
|
||||
child: MenuAnchor(
|
||||
controller: _menuController,
|
||||
anchorTapClosesMenu: true,
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
child: Text(MenuEntry.about.label),
|
||||
onPressed: () => _activate(MenuEntry.about),
|
||||
),
|
||||
if (_showingMessage) MenuItemButton(
|
||||
onPressed: () => _activate(MenuEntry.hideMessage),
|
||||
shortcut: MenuEntry.hideMessage.shortcut,
|
||||
child: Text(MenuEntry.hideMessage.label),
|
||||
),
|
||||
if (!_showingMessage) MenuItemButton(
|
||||
onPressed: () => _activate(MenuEntry.showMessage),
|
||||
shortcut: MenuEntry.showMessage.shortcut,
|
||||
child: Text(MenuEntry.showMessage.label),
|
||||
),
|
||||
SubmenuButton(
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
onPressed: () => _activate(MenuEntry.colorRed),
|
||||
shortcut: MenuEntry.colorRed.shortcut,
|
||||
child: Text(MenuEntry.colorRed.label),
|
||||
),
|
||||
MenuItemButton(
|
||||
onPressed: () => _activate(MenuEntry.colorGreen),
|
||||
shortcut: MenuEntry.colorGreen.shortcut,
|
||||
child: Text(MenuEntry.colorGreen.label),
|
||||
),
|
||||
MenuItemButton(
|
||||
onPressed: () => _activate(MenuEntry.colorBlue),
|
||||
shortcut: MenuEntry.colorBlue.shortcut,
|
||||
child: Text(MenuEntry.colorBlue.label),
|
||||
),
|
||||
],
|
||||
child: const Text('Background Color'),
|
||||
),
|
||||
],
|
||||
child: Container(
|
||||
alignment: Alignment.center,
|
||||
color: backgroundColor,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(8.0),
|
||||
child: Text('Ctrl-click anywhere on the background to show the menu.'),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: Text(
|
||||
showingMessage ? widget.message : '',
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
),
|
||||
Text(_lastSelection != null ? 'Last Selected: ${_lastSelection!.label}' : ''),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _activate(MenuEntry selection) {
|
||||
setState(() {
|
||||
_lastSelection = selection;
|
||||
});
|
||||
switch (selection) {
|
||||
case MenuEntry.about:
|
||||
showAboutDialog(
|
||||
context: context,
|
||||
applicationName: 'MenuBar Sample',
|
||||
applicationVersion: '1.0.0',
|
||||
);
|
||||
break;
|
||||
case MenuEntry.showMessage:
|
||||
case MenuEntry.hideMessage:
|
||||
showingMessage = !showingMessage;
|
||||
break;
|
||||
case MenuEntry.colorMenu:
|
||||
break;
|
||||
case MenuEntry.colorRed:
|
||||
backgroundColor = Colors.red;
|
||||
break;
|
||||
case MenuEntry.colorGreen:
|
||||
backgroundColor = Colors.green;
|
||||
break;
|
||||
case MenuEntry.colorBlue:
|
||||
backgroundColor = Colors.blue;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void _handleTapDown(TapDownDetails details) {
|
||||
if (!HardwareKeyboard.instance.logicalKeysPressed.contains(LogicalKeyboardKey.controlLeft) &&
|
||||
!HardwareKeyboard.instance.logicalKeysPressed.contains(LogicalKeyboardKey.controlRight)) {
|
||||
return;
|
||||
}
|
||||
_menuController.open(position: details.localPosition);
|
||||
}
|
||||
}
|
||||
|
||||
class ContextMenuApp extends StatelessWidget {
|
||||
const ContextMenuApp({super.key});
|
||||
|
||||
static const String kMessage = '"Talk less. Smile more." - A. Burr';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: Scaffold(body: MyContextMenu(message: kMessage)),
|
||||
);
|
||||
}
|
||||
}
|
||||
236
examples/api/lib/material/menu_anchor/menu_bar.0.dart
Normal file
236
examples/api/lib/material/menu_anchor/menu_bar.0.dart
Normal file
@@ -0,0 +1,236 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
/// Flutter code sample for [MenuBar]
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
void main() => runApp(const MenuBarApp());
|
||||
|
||||
/// A class for consolidating the definition of menu entries.
|
||||
///
|
||||
/// This sort of class is not required, but illustrates one way that defining
|
||||
/// menus could be done.
|
||||
class MenuEntry {
|
||||
const MenuEntry({required this.label, this.shortcut, this.onPressed, this.menuChildren})
|
||||
: assert(menuChildren == null || onPressed == null, 'onPressed is ignored if menuChildren are provided');
|
||||
final String label;
|
||||
|
||||
final MenuSerializableShortcut? shortcut;
|
||||
final VoidCallback? onPressed;
|
||||
final List<MenuEntry>? menuChildren;
|
||||
|
||||
static List<Widget> build(List<MenuEntry> selections) {
|
||||
Widget buildSelection(MenuEntry selection) {
|
||||
if (selection.menuChildren != null) {
|
||||
return SubmenuButton(
|
||||
menuChildren: MenuEntry.build(selection.menuChildren!),
|
||||
child: Text(selection.label),
|
||||
);
|
||||
}
|
||||
return MenuItemButton(
|
||||
shortcut: selection.shortcut,
|
||||
onPressed: selection.onPressed,
|
||||
child: Text(selection.label),
|
||||
);
|
||||
}
|
||||
|
||||
return selections.map<Widget>(buildSelection).toList();
|
||||
}
|
||||
|
||||
static Map<MenuSerializableShortcut, Intent> shortcuts(List<MenuEntry> selections) {
|
||||
final Map<MenuSerializableShortcut, Intent> result = <MenuSerializableShortcut, Intent>{};
|
||||
for (final MenuEntry selection in selections) {
|
||||
if (selection.menuChildren != null) {
|
||||
result.addAll(MenuEntry.shortcuts(selection.menuChildren!));
|
||||
} else {
|
||||
if (selection.shortcut != null && selection.onPressed != null) {
|
||||
result[selection.shortcut!] = VoidCallbackIntent(selection.onPressed!);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
class MyMenuBar extends StatefulWidget {
|
||||
const MyMenuBar({
|
||||
super.key,
|
||||
required this.message,
|
||||
});
|
||||
|
||||
final String message;
|
||||
|
||||
@override
|
||||
State<MyMenuBar> createState() => _MyMenuBarState();
|
||||
}
|
||||
|
||||
class _MyMenuBarState extends State<MyMenuBar> {
|
||||
ShortcutRegistryEntry? _shortcutsEntry;
|
||||
String? _lastSelection;
|
||||
|
||||
Color get backgroundColor => _backgroundColor;
|
||||
Color _backgroundColor = Colors.red;
|
||||
set backgroundColor(Color value) {
|
||||
if (_backgroundColor != value) {
|
||||
setState(() {
|
||||
_backgroundColor = value;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
bool get showingMessage => _showMessage;
|
||||
bool _showMessage = false;
|
||||
set showingMessage(bool value) {
|
||||
if (_showMessage != value) {
|
||||
setState(() {
|
||||
_showMessage = value;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_shortcutsEntry?.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: MenuBar(
|
||||
children: MenuEntry.build(_getMenus()),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: Container(
|
||||
alignment: Alignment.center,
|
||||
color: backgroundColor,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: Text(
|
||||
showingMessage ? widget.message : '',
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
),
|
||||
Text(_lastSelection != null ? 'Last Selected: $_lastSelection' : ''),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
List<MenuEntry> _getMenus() {
|
||||
final List<MenuEntry> result = <MenuEntry>[
|
||||
MenuEntry(
|
||||
label: 'Menu Demo',
|
||||
menuChildren: <MenuEntry>[
|
||||
MenuEntry(
|
||||
label: 'About',
|
||||
onPressed: () {
|
||||
showAboutDialog(
|
||||
context: context,
|
||||
applicationName: 'MenuBar Sample',
|
||||
applicationVersion: '1.0.0',
|
||||
);
|
||||
setState(() {
|
||||
_lastSelection = 'About';
|
||||
});
|
||||
},
|
||||
),
|
||||
MenuEntry(
|
||||
label: showingMessage ? 'Hide Message' : 'Show Message',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_lastSelection = showingMessage ? 'Hide Message' : 'Show Message';
|
||||
showingMessage = !showingMessage;
|
||||
});
|
||||
},
|
||||
shortcut: const SingleActivator(LogicalKeyboardKey.keyS, control: true),
|
||||
),
|
||||
// Hides the message, but is only enabled if the message isn't
|
||||
// already hidden.
|
||||
MenuEntry(
|
||||
label: 'Reset Message',
|
||||
onPressed: showingMessage
|
||||
? () {
|
||||
setState(() {
|
||||
_lastSelection = 'Reset Message';
|
||||
showingMessage = false;
|
||||
});
|
||||
}
|
||||
: null,
|
||||
shortcut: const SingleActivator(LogicalKeyboardKey.escape),
|
||||
),
|
||||
MenuEntry(
|
||||
label: 'Background Color',
|
||||
menuChildren: <MenuEntry>[
|
||||
MenuEntry(
|
||||
label: 'Red Background',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_lastSelection = 'Red Background';
|
||||
backgroundColor = Colors.red;
|
||||
});
|
||||
},
|
||||
shortcut: const SingleActivator(LogicalKeyboardKey.keyR, control: true),
|
||||
),
|
||||
MenuEntry(
|
||||
label: 'Green Background',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_lastSelection = 'Green Background';
|
||||
backgroundColor = Colors.green;
|
||||
});
|
||||
},
|
||||
shortcut: const SingleActivator(LogicalKeyboardKey.keyG, control: true),
|
||||
),
|
||||
MenuEntry(
|
||||
label: 'Blue Background',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_lastSelection = 'Blue Background';
|
||||
backgroundColor = Colors.blue;
|
||||
});
|
||||
},
|
||||
shortcut: const SingleActivator(LogicalKeyboardKey.keyB, control: true),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
];
|
||||
// (Re-)register the shortcuts with the ShortcutRegistry so that they are
|
||||
// available to the entire application, and update them if they've changed.
|
||||
_shortcutsEntry?.dispose();
|
||||
_shortcutsEntry = ShortcutRegistry.of(context).addAll(MenuEntry.shortcuts(result));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
class MenuBarApp extends StatelessWidget {
|
||||
const MenuBarApp({super.key});
|
||||
|
||||
static const String kMessage = '"Talk less. Smile more." - A. Burr';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: Scaffold(body: MyMenuBar(message: kMessage)),
|
||||
);
|
||||
}
|
||||
}
|
||||
107
examples/api/test/material/menu_anchor/menu_anchor.0_test.dart
Normal file
107
examples/api/test/material/menu_anchor/menu_anchor.0_test.dart
Normal file
@@ -0,0 +1,107 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_api_samples/material/menu_anchor/menu_anchor.0.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Can open menu', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.MenuApp(),
|
||||
);
|
||||
|
||||
await tester.tap(find.byType(TextButton));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuEntry.about.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.showMessage.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.hideMessage.label), findsNothing);
|
||||
expect(find.text('Background Color'), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.colorRed.label), findsNothing);
|
||||
expect(find.text(example.MenuEntry.colorGreen.label), findsNothing);
|
||||
expect(find.text(example.MenuEntry.colorBlue.label), findsNothing);
|
||||
expect(find.text(example.MenuApp.kMessage), findsNothing);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Background Color'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('Background Color'));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuEntry.colorRed.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.colorGreen.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.colorBlue.label), findsOneWidget);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuApp.kMessage), findsOneWidget);
|
||||
expect(find.text('Last Selected: ${example.MenuEntry.showMessage.label}'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Shortcuts work', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.MenuApp(),
|
||||
);
|
||||
|
||||
// Open the menu so we can watch state changes resulting from the shortcuts
|
||||
// firing.
|
||||
await tester.tap(find.byType(TextButton));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuEntry.showMessage.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.hideMessage.label), findsNothing);
|
||||
expect(find.text(example.MenuApp.kMessage), findsNothing);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyS);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
// Need to pump twice because of the one frame delay in the notification to
|
||||
// update the overlay entry.
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuEntry.showMessage.label), findsNothing);
|
||||
expect(find.text(example.MenuEntry.hideMessage.label), findsOneWidget);
|
||||
expect(find.text(example.MenuApp.kMessage), findsOneWidget);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyS);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuEntry.showMessage.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.hideMessage.label), findsNothing);
|
||||
expect(find.text(example.MenuApp.kMessage), findsNothing);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyR);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Last Selected: ${example.MenuEntry.colorRed.label}'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Last Selected: ${example.MenuEntry.colorGreen.label}'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyB);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Last Selected: ${example.MenuEntry.colorBlue.label}'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
121
examples/api/test/material/menu_anchor/menu_anchor.1_test.dart
Normal file
121
examples/api/test/material/menu_anchor/menu_anchor.1_test.dart
Normal file
@@ -0,0 +1,121 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_api_samples/material/menu_anchor/menu_anchor.1.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Can open menu', (WidgetTester tester) async {
|
||||
Finder findMenu() {
|
||||
return find.ancestor(
|
||||
of: find.text(example.MenuEntry.about.label),
|
||||
matching: find.byType(FocusScope),
|
||||
).first;
|
||||
}
|
||||
|
||||
await tester.pumpWidget(const example.ContextMenuApp());
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlRight);
|
||||
await tester.tapAt(const Offset(100, 200));
|
||||
await tester.pump();
|
||||
expect(tester.getRect(findMenu()), equals(const Rect.fromLTRB(100.0, 200.0, 404.0, 352.0)));
|
||||
|
||||
// Make sure tapping in a different place causes the menu to move.
|
||||
await tester.tapAt(const Offset(200, 100));
|
||||
await tester.pump();
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlRight);
|
||||
|
||||
expect(tester.getRect(findMenu()), equals(const Rect.fromLTRB(200.0, 100.0, 504.0, 252.0)));
|
||||
|
||||
expect(find.text(example.MenuEntry.about.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.showMessage.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.hideMessage.label), findsNothing);
|
||||
expect(find.text('Background Color'), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.colorRed.label), findsNothing);
|
||||
expect(find.text(example.MenuEntry.colorGreen.label), findsNothing);
|
||||
expect(find.text(example.MenuEntry.colorBlue.label), findsNothing);
|
||||
expect(find.text(example.ContextMenuApp.kMessage), findsNothing);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Background Color'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('Background Color'));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuEntry.colorRed.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.colorGreen.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.colorBlue.label), findsOneWidget);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.ContextMenuApp.kMessage), findsOneWidget);
|
||||
expect(find.text('Last Selected: ${example.MenuEntry.showMessage.label}'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Shortcuts work', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.ContextMenuApp(),
|
||||
);
|
||||
|
||||
// Open the menu so we can look for state changes reflected in the menu.
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlRight);
|
||||
await tester.tapAt(const Offset(100, 200));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuEntry.showMessage.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.hideMessage.label), findsNothing);
|
||||
expect(find.text(example.ContextMenuApp.kMessage), findsNothing);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyS);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
// Need to pump twice because of the one frame delay in the notification to
|
||||
// update the overlay entry.
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuEntry.showMessage.label), findsNothing);
|
||||
expect(find.text(example.MenuEntry.hideMessage.label), findsOneWidget);
|
||||
expect(find.text(example.ContextMenuApp.kMessage), findsOneWidget);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyS);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuEntry.showMessage.label), findsOneWidget);
|
||||
expect(find.text(example.MenuEntry.hideMessage.label), findsNothing);
|
||||
expect(find.text(example.ContextMenuApp.kMessage), findsNothing);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyR);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Last Selected: ${example.MenuEntry.colorRed.label}'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Last Selected: ${example.MenuEntry.colorGreen.label}'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyB);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Last Selected: ${example.MenuEntry.colorBlue.label}'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
94
examples/api/test/material/menu_anchor/menu_bar.0_test.dart
Normal file
94
examples/api/test/material/menu_anchor/menu_bar.0_test.dart
Normal file
@@ -0,0 +1,94 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_api_samples/material/menu_anchor/menu_bar.0.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Can open menu', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.MenuBarApp(),
|
||||
);
|
||||
|
||||
final Finder menuBarFinder = find.byType(MenuBar);
|
||||
final MenuBar menuBar = tester.widget<MenuBar>(menuBarFinder);
|
||||
expect(menuBar.children, isNotEmpty);
|
||||
expect(menuBar.children.length, equals(1));
|
||||
|
||||
final Finder menuButtonFinder = find.byType(SubmenuButton).first;
|
||||
await tester.tap(menuButtonFinder);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('About'), findsOneWidget);
|
||||
expect(find.text('Show Message'), findsOneWidget);
|
||||
expect(find.text('Reset Message'), findsOneWidget);
|
||||
expect(find.text('Background Color'), findsOneWidget);
|
||||
expect(find.text('Red Background'), findsNothing);
|
||||
expect(find.text('Green Background'), findsNothing);
|
||||
expect(find.text('Blue Background'), findsNothing);
|
||||
expect(find.text(example.MenuBarApp.kMessage), findsNothing);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('About'), findsOneWidget);
|
||||
expect(find.text('Show Message'), findsOneWidget);
|
||||
expect(find.text('Reset Message'), findsOneWidget);
|
||||
expect(find.text('Background Color'), findsOneWidget);
|
||||
expect(find.text('Red Background'), findsOneWidget);
|
||||
expect(find.text('Green Background'), findsOneWidget);
|
||||
expect(find.text('Blue Background'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuBarApp.kMessage), findsOneWidget);
|
||||
expect(find.text('Last Selected: Show Message'), findsOneWidget);
|
||||
});
|
||||
testWidgets('Shortcuts work', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.MenuBarApp(),
|
||||
);
|
||||
|
||||
expect(find.text(example.MenuBarApp.kMessage), findsNothing);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyS);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuBarApp.kMessage), findsOneWidget);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(example.MenuBarApp.kMessage), findsNothing);
|
||||
expect(find.text('Last Selected: Reset Message'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyR);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Last Selected: Red Background'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Last Selected: Green Background'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyB);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Last Selected: Blue Background'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user