Menu bar accelerators (#114852)
* Add MenuMenuAcceleratorLabel to support accelerators. * Review Changes * Review Changed * Fix default label builder to use characters * Remove golden test that shouldn't have been there.
This commit is contained in:
@@ -29,6 +29,7 @@ class _HomeState extends State<Home> {
|
||||
TextDirection _textDirection = TextDirection.ltr;
|
||||
double _extraPadding = 0;
|
||||
bool _addItem = false;
|
||||
bool _accelerators = true;
|
||||
bool _transparent = false;
|
||||
bool _funkyTheme = false;
|
||||
|
||||
@@ -99,6 +100,7 @@ class _HomeState extends State<Home> {
|
||||
children: <Widget>[
|
||||
_TestMenus(
|
||||
menuController: _controller,
|
||||
accelerators: _accelerators,
|
||||
addItem: _addItem,
|
||||
),
|
||||
Expanded(
|
||||
@@ -107,6 +109,7 @@ class _HomeState extends State<Home> {
|
||||
menuController: _controller,
|
||||
density: _density,
|
||||
addItem: _addItem,
|
||||
accelerators: _accelerators,
|
||||
transparent: _transparent,
|
||||
funkyTheme: _funkyTheme,
|
||||
extraPadding: _extraPadding,
|
||||
@@ -131,6 +134,11 @@ class _HomeState extends State<Home> {
|
||||
_addItem = value;
|
||||
});
|
||||
},
|
||||
onAcceleratorsChanged: (bool value) {
|
||||
setState(() {
|
||||
_accelerators = value;
|
||||
});
|
||||
},
|
||||
onTransparentChanged: (bool value) {
|
||||
setState(() {
|
||||
_transparent = value;
|
||||
@@ -159,12 +167,14 @@ class _Controls extends StatefulWidget {
|
||||
required this.textDirection,
|
||||
required this.extraPadding,
|
||||
this.addItem = false,
|
||||
this.accelerators = true,
|
||||
this.transparent = false,
|
||||
this.funkyTheme = false,
|
||||
required this.onDensityChanged,
|
||||
required this.onTextDirectionChanged,
|
||||
required this.onExtraPaddingChanged,
|
||||
required this.onAddItemChanged,
|
||||
required this.onAcceleratorsChanged,
|
||||
required this.onTransparentChanged,
|
||||
required this.onFunkyThemeChanged,
|
||||
required this.menuController,
|
||||
@@ -174,12 +184,14 @@ class _Controls extends StatefulWidget {
|
||||
final TextDirection textDirection;
|
||||
final double extraPadding;
|
||||
final bool addItem;
|
||||
final bool accelerators;
|
||||
final bool transparent;
|
||||
final bool funkyTheme;
|
||||
final ValueChanged<VisualDensity> onDensityChanged;
|
||||
final ValueChanged<TextDirection> onTextDirectionChanged;
|
||||
final ValueChanged<double> onExtraPaddingChanged;
|
||||
final ValueChanged<bool> onAddItemChanged;
|
||||
final ValueChanged<bool> onAcceleratorsChanged;
|
||||
final ValueChanged<bool> onTransparentChanged;
|
||||
final ValueChanged<bool> onFunkyThemeChanged;
|
||||
final MenuController menuController;
|
||||
@@ -199,165 +211,180 @@ class _ControlsState extends State<_Controls> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
color: Colors.lightBlueAccent,
|
||||
alignment: Alignment.center,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
MenuAnchor(
|
||||
childFocusNode: _focusNode,
|
||||
style: const MenuStyle(alignment: AlignmentDirectional.topEnd),
|
||||
alignmentOffset: const Offset(100, -8),
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
shortcut: TestMenu.standaloneMenu1.shortcut,
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.standaloneMenu1);
|
||||
},
|
||||
child: Text(TestMenu.standaloneMenu1.label),
|
||||
return Center(
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
MenuAnchor(
|
||||
childFocusNode: _focusNode,
|
||||
style: const MenuStyle(alignment: AlignmentDirectional.topEnd),
|
||||
alignmentOffset: const Offset(100, -8),
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
shortcut: TestMenu.standaloneMenu1.shortcut,
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.standaloneMenu1);
|
||||
},
|
||||
child: MenuAcceleratorLabel(TestMenu.standaloneMenu1.label),
|
||||
),
|
||||
MenuItemButton(
|
||||
leadingIcon: const Icon(Icons.send),
|
||||
trailingIcon: const Icon(Icons.mail),
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.standaloneMenu2);
|
||||
},
|
||||
child: MenuAcceleratorLabel(TestMenu.standaloneMenu2.label),
|
||||
),
|
||||
],
|
||||
builder: (BuildContext context, MenuController controller, Widget? child) {
|
||||
return TextButton(
|
||||
focusNode: _focusNode,
|
||||
onPressed: () {
|
||||
if (controller.isOpen) {
|
||||
controller.close();
|
||||
} else {
|
||||
controller.open();
|
||||
}
|
||||
},
|
||||
child: child!,
|
||||
);
|
||||
},
|
||||
child: const MenuAcceleratorLabel('Open Menu'),
|
||||
),
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 400),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: <Widget>[
|
||||
_ControlSlider(
|
||||
label: 'Extra Padding: ${widget.extraPadding.toStringAsFixed(1)}',
|
||||
value: widget.extraPadding,
|
||||
max: 40,
|
||||
divisions: 20,
|
||||
onChanged: (double value) {
|
||||
widget.onExtraPaddingChanged(value);
|
||||
},
|
||||
),
|
||||
_ControlSlider(
|
||||
label: 'Horizontal Density: ${widget.density.horizontal.toStringAsFixed(1)}',
|
||||
value: widget.density.horizontal,
|
||||
max: 4,
|
||||
min: -4,
|
||||
divisions: 12,
|
||||
onChanged: (double value) {
|
||||
widget.onDensityChanged(
|
||||
VisualDensity(
|
||||
horizontal: value,
|
||||
vertical: widget.density.vertical,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
_ControlSlider(
|
||||
label: 'Vertical Density: ${widget.density.vertical.toStringAsFixed(1)}',
|
||||
value: widget.density.vertical,
|
||||
max: 4,
|
||||
min: -4,
|
||||
divisions: 12,
|
||||
onChanged: (double value) {
|
||||
widget.onDensityChanged(
|
||||
VisualDensity(
|
||||
horizontal: widget.density.horizontal,
|
||||
vertical: value,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
MenuItemButton(
|
||||
leadingIcon: const Icon(Icons.send),
|
||||
trailingIcon: const Icon(Icons.mail),
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.standaloneMenu2);
|
||||
},
|
||||
child: Text(TestMenu.standaloneMenu2.label),
|
||||
),
|
||||
],
|
||||
builder: (BuildContext context, MenuController controller, Widget? child) {
|
||||
return TextButton(
|
||||
focusNode: _focusNode,
|
||||
onPressed: () {
|
||||
if (controller.isOpen) {
|
||||
controller.close();
|
||||
} else {
|
||||
controller.open();
|
||||
}
|
||||
},
|
||||
child: child!,
|
||||
);
|
||||
},
|
||||
child: const Text('Open Menu'),
|
||||
),
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 400),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
_ControlSlider(
|
||||
label: 'Extra Padding: ${widget.extraPadding.toStringAsFixed(1)}',
|
||||
value: widget.extraPadding,
|
||||
max: 40,
|
||||
divisions: 20,
|
||||
onChanged: (double value) {
|
||||
widget.onExtraPaddingChanged(value);
|
||||
},
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Checkbox(
|
||||
value: widget.textDirection == TextDirection.rtl,
|
||||
onChanged: (bool? value) {
|
||||
if (value ?? false) {
|
||||
widget.onTextDirectionChanged(TextDirection.rtl);
|
||||
} else {
|
||||
widget.onTextDirectionChanged(TextDirection.ltr);
|
||||
}
|
||||
},
|
||||
),
|
||||
const Text('RTL Text')
|
||||
],
|
||||
),
|
||||
_ControlSlider(
|
||||
label: 'Horizontal Density: ${widget.density.horizontal.toStringAsFixed(1)}',
|
||||
value: widget.density.horizontal,
|
||||
max: 4,
|
||||
min: -4,
|
||||
divisions: 12,
|
||||
onChanged: (double value) {
|
||||
widget.onDensityChanged(
|
||||
VisualDensity(
|
||||
horizontal: value,
|
||||
vertical: widget.density.vertical,
|
||||
),
|
||||
);
|
||||
},
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Checkbox(
|
||||
value: widget.addItem,
|
||||
onChanged: (bool? value) {
|
||||
if (value ?? false) {
|
||||
widget.onAddItemChanged(true);
|
||||
} else {
|
||||
widget.onAddItemChanged(false);
|
||||
}
|
||||
},
|
||||
),
|
||||
const Text('Add Item')
|
||||
],
|
||||
),
|
||||
_ControlSlider(
|
||||
label: 'Vertical Density: ${widget.density.vertical.toStringAsFixed(1)}',
|
||||
value: widget.density.vertical,
|
||||
max: 4,
|
||||
min: -4,
|
||||
divisions: 12,
|
||||
onChanged: (double value) {
|
||||
widget.onDensityChanged(
|
||||
VisualDensity(
|
||||
horizontal: widget.density.horizontal,
|
||||
vertical: value,
|
||||
),
|
||||
);
|
||||
},
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Checkbox(
|
||||
value: widget.accelerators,
|
||||
onChanged: (bool? value) {
|
||||
if (value ?? false) {
|
||||
widget.onAcceleratorsChanged(true);
|
||||
} else {
|
||||
widget.onAcceleratorsChanged(false);
|
||||
}
|
||||
},
|
||||
),
|
||||
const Text('Enable Accelerators')
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Checkbox(
|
||||
value: widget.transparent,
|
||||
onChanged: (bool? value) {
|
||||
if (value ?? false) {
|
||||
widget.onTransparentChanged(true);
|
||||
} else {
|
||||
widget.onTransparentChanged(false);
|
||||
}
|
||||
},
|
||||
),
|
||||
const Text('Transparent')
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Checkbox(
|
||||
value: widget.funkyTheme,
|
||||
onChanged: (bool? value) {
|
||||
if (value ?? false) {
|
||||
widget.onFunkyThemeChanged(true);
|
||||
} else {
|
||||
widget.onFunkyThemeChanged(false);
|
||||
}
|
||||
},
|
||||
),
|
||||
const Text('Funky Theme')
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Checkbox(
|
||||
value: widget.textDirection == TextDirection.rtl,
|
||||
onChanged: (bool? value) {
|
||||
if (value ?? false) {
|
||||
widget.onTextDirectionChanged(TextDirection.rtl);
|
||||
} else {
|
||||
widget.onTextDirectionChanged(TextDirection.ltr);
|
||||
}
|
||||
},
|
||||
),
|
||||
const Text('RTL Text')
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Checkbox(
|
||||
value: widget.addItem,
|
||||
onChanged: (bool? value) {
|
||||
if (value ?? false) {
|
||||
widget.onAddItemChanged(true);
|
||||
} else {
|
||||
widget.onAddItemChanged(false);
|
||||
}
|
||||
},
|
||||
),
|
||||
const Text('Add Item')
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Checkbox(
|
||||
value: widget.transparent,
|
||||
onChanged: (bool? value) {
|
||||
if (value ?? false) {
|
||||
widget.onTransparentChanged(true);
|
||||
} else {
|
||||
widget.onTransparentChanged(false);
|
||||
}
|
||||
},
|
||||
),
|
||||
const Text('Transparent')
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Checkbox(
|
||||
value: widget.funkyTheme,
|
||||
onChanged: (bool? value) {
|
||||
if (value ?? false) {
|
||||
widget.onFunkyThemeChanged(true);
|
||||
} else {
|
||||
widget.onFunkyThemeChanged(false);
|
||||
}
|
||||
},
|
||||
),
|
||||
const Text('Funky Theme')
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -412,10 +439,12 @@ class _TestMenus extends StatefulWidget {
|
||||
const _TestMenus({
|
||||
required this.menuController,
|
||||
this.addItem = false,
|
||||
this.accelerators = false,
|
||||
});
|
||||
|
||||
final MenuController menuController;
|
||||
final bool addItem;
|
||||
final bool accelerators;
|
||||
|
||||
@override
|
||||
State<_TestMenus> createState() => _TestMenusState();
|
||||
@@ -439,8 +468,8 @@ class _TestMenusState extends State<_TestMenus> {
|
||||
debugPrint('App: Closed item ${item.label}');
|
||||
}
|
||||
|
||||
void _setRadio(TestMenu item) {
|
||||
debugPrint('App: Set Radio item ${item.label}');
|
||||
void _setRadio(TestMenu? item) {
|
||||
debugPrint('App: Set Radio item ${item?.label}');
|
||||
setState(() {
|
||||
radioValue = item;
|
||||
});
|
||||
@@ -449,17 +478,17 @@ class _TestMenusState extends State<_TestMenus> {
|
||||
void _setCheck(TestMenu item) {
|
||||
debugPrint('App: Set Checkbox item ${item.label}');
|
||||
setState(() {
|
||||
switch (checkboxState) {
|
||||
case false:
|
||||
checkboxState = true;
|
||||
break;
|
||||
case true:
|
||||
checkboxState = null;
|
||||
break;
|
||||
case null:
|
||||
checkboxState = false;
|
||||
break;
|
||||
}
|
||||
switch (checkboxState) {
|
||||
case false:
|
||||
checkboxState = true;
|
||||
break;
|
||||
case true:
|
||||
checkboxState = null;
|
||||
break;
|
||||
case null:
|
||||
checkboxState = false;
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -469,9 +498,9 @@ class _TestMenusState extends State<_TestMenus> {
|
||||
_shortcutsEntry?.dispose();
|
||||
final Map<ShortcutActivator, Intent> shortcuts = <ShortcutActivator, Intent>{};
|
||||
for (final TestMenu item in TestMenu.values) {
|
||||
if (item.shortcut == null) {
|
||||
if (item.shortcut == null) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
switch (item) {
|
||||
case TestMenu.radioMenu1:
|
||||
case TestMenu.radioMenu2:
|
||||
@@ -519,219 +548,21 @@ class _TestMenusState extends State<_TestMenus> {
|
||||
Expanded(
|
||||
child: MenuBar(
|
||||
controller: widget.menuController,
|
||||
children: <Widget>[
|
||||
SubmenuButton(
|
||||
onOpen: () {
|
||||
_openItem(TestMenu.mainMenu1);
|
||||
},
|
||||
onClose: () {
|
||||
_closeItem(TestMenu.mainMenu1);
|
||||
},
|
||||
menuChildren: <Widget>[
|
||||
CheckboxMenuButton(
|
||||
value: checkboxState,
|
||||
tristate: true,
|
||||
shortcut: TestMenu.subMenu1.shortcut,
|
||||
trailingIcon: const Icon(Icons.assessment),
|
||||
onChanged: (bool? value) {
|
||||
setState(() {
|
||||
checkboxState = value;
|
||||
});
|
||||
_itemSelected(TestMenu.subMenu1);
|
||||
},
|
||||
child: Text(TestMenu.subMenu1.label),
|
||||
),
|
||||
RadioMenuButton<TestMenu>(
|
||||
value: TestMenu.radioMenu1,
|
||||
groupValue: radioValue,
|
||||
toggleable: true,
|
||||
shortcut: TestMenu.radioMenu1.shortcut,
|
||||
trailingIcon: const Icon(Icons.assessment),
|
||||
onChanged: (TestMenu? value) {
|
||||
setState(() {
|
||||
radioValue = value;
|
||||
});
|
||||
_itemSelected(TestMenu.radioMenu1);
|
||||
},
|
||||
child: Text(TestMenu.radioMenu1.label),
|
||||
),
|
||||
RadioMenuButton<TestMenu>(
|
||||
value: TestMenu.radioMenu2,
|
||||
groupValue: radioValue,
|
||||
toggleable: true,
|
||||
shortcut: TestMenu.radioMenu2.shortcut,
|
||||
trailingIcon: const Icon(Icons.assessment),
|
||||
onChanged: (TestMenu? value) {
|
||||
setState(() {
|
||||
radioValue = value;
|
||||
});
|
||||
_itemSelected(TestMenu.radioMenu2);
|
||||
},
|
||||
child: Text(TestMenu.radioMenu2.label),
|
||||
),
|
||||
RadioMenuButton<TestMenu>(
|
||||
value: TestMenu.radioMenu3,
|
||||
groupValue: radioValue,
|
||||
toggleable: true,
|
||||
shortcut: TestMenu.radioMenu3.shortcut,
|
||||
trailingIcon: const Icon(Icons.assessment),
|
||||
onChanged: (TestMenu? value) {
|
||||
setState(() {
|
||||
radioValue = value;
|
||||
});
|
||||
_itemSelected(TestMenu.radioMenu3);
|
||||
},
|
||||
child: Text(TestMenu.radioMenu3.label),
|
||||
),
|
||||
MenuItemButton(
|
||||
leadingIcon: const Icon(Icons.send),
|
||||
trailingIcon: const Icon(Icons.mail),
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.subMenu2);
|
||||
},
|
||||
child: Text(TestMenu.subMenu2.label),
|
||||
),
|
||||
],
|
||||
child: Text(TestMenu.mainMenu1.label),
|
||||
),
|
||||
SubmenuButton(
|
||||
onOpen: () {
|
||||
_openItem(TestMenu.mainMenu2);
|
||||
},
|
||||
onClose: () {
|
||||
_closeItem(TestMenu.mainMenu2);
|
||||
},
|
||||
menuChildren: <Widget>[
|
||||
TextButton(
|
||||
child: const Text('TEST'),
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.testButton);
|
||||
widget.menuController.close();
|
||||
},
|
||||
),
|
||||
MenuItemButton(
|
||||
shortcut: TestMenu.subMenu3.shortcut,
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.subMenu3);
|
||||
},
|
||||
child: Text(TestMenu.subMenu3.label),
|
||||
),
|
||||
],
|
||||
child: Text(TestMenu.mainMenu2.label),
|
||||
),
|
||||
SubmenuButton(
|
||||
onOpen: () {
|
||||
_openItem(TestMenu.mainMenu3);
|
||||
},
|
||||
onClose: () {
|
||||
_closeItem(TestMenu.mainMenu3);
|
||||
},
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
child: Text(TestMenu.subMenu8.label),
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.subMenu8);
|
||||
},
|
||||
),
|
||||
],
|
||||
child: Text(TestMenu.mainMenu3.label),
|
||||
),
|
||||
SubmenuButton(
|
||||
onOpen: () {
|
||||
_openItem(TestMenu.mainMenu4);
|
||||
},
|
||||
onClose: () {
|
||||
_closeItem(TestMenu.mainMenu4);
|
||||
},
|
||||
menuChildren: <Widget>[
|
||||
Actions(
|
||||
actions: <Type, Action<Intent>>{
|
||||
ActivateIntent: CallbackAction<ActivateIntent>(
|
||||
onInvoke: (ActivateIntent? intent) {
|
||||
debugPrint('Activated!');
|
||||
return;
|
||||
},
|
||||
)
|
||||
},
|
||||
child: MenuItemButton(
|
||||
onPressed: () {
|
||||
debugPrint('Activated text input item with ${textController.text} as a value.');
|
||||
},
|
||||
child: SizedBox(
|
||||
width: 200,
|
||||
child: TextField(
|
||||
controller: textController,
|
||||
onSubmitted: (String value) {
|
||||
debugPrint('String $value submitted.');
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SubmenuButton(
|
||||
onOpen: () {
|
||||
_openItem(TestMenu.subMenu5);
|
||||
},
|
||||
onClose: () {
|
||||
_closeItem(TestMenu.subMenu5);
|
||||
},
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
shortcut: TestMenu.subSubMenu1.shortcut,
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.subSubMenu1);
|
||||
},
|
||||
child: Text(TestMenu.subSubMenu1.label),
|
||||
),
|
||||
MenuItemButton(
|
||||
child: Text(TestMenu.subSubMenu2.label),
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.subSubMenu2);
|
||||
},
|
||||
),
|
||||
if (widget.addItem)
|
||||
SubmenuButton(
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
shortcut: TestMenu.subSubSubMenu1.shortcut,
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.subSubSubMenu1);
|
||||
},
|
||||
child: Text(TestMenu.subSubSubMenu1.label),
|
||||
),
|
||||
],
|
||||
child: Text(TestMenu.subSubMenu3.label),
|
||||
),
|
||||
],
|
||||
child: Text(TestMenu.subMenu5.label),
|
||||
),
|
||||
MenuItemButton(
|
||||
// Disabled button
|
||||
shortcut: TestMenu.subMenu6.shortcut,
|
||||
child: Text(TestMenu.subMenu6.label),
|
||||
),
|
||||
MenuItemButton(
|
||||
child: Text(TestMenu.subMenu7.label),
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.subMenu7);
|
||||
},
|
||||
),
|
||||
MenuItemButton(
|
||||
child: Text(TestMenu.subMenu7.label),
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.subMenu7);
|
||||
},
|
||||
),
|
||||
MenuItemButton(
|
||||
child: Text(TestMenu.subMenu8.label),
|
||||
onPressed: () {
|
||||
_itemSelected(TestMenu.subMenu8);
|
||||
},
|
||||
),
|
||||
],
|
||||
child: Text(TestMenu.mainMenu4.label),
|
||||
),
|
||||
],
|
||||
children: createTestMenus(
|
||||
onPressed: _itemSelected,
|
||||
onOpen: _openItem,
|
||||
onClose: _closeItem,
|
||||
onCheckboxChanged: (TestMenu menu, bool? value) {
|
||||
_setCheck(menu);
|
||||
},
|
||||
onRadioChanged: _setRadio,
|
||||
checkboxValue: checkboxState,
|
||||
radioValue: radioValue,
|
||||
menuController: widget.menuController,
|
||||
textEditingController: textController,
|
||||
includeExtraGroups: widget.addItem,
|
||||
accelerators: widget.accelerators,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -739,31 +570,223 @@ class _TestMenusState extends State<_TestMenus> {
|
||||
}
|
||||
}
|
||||
|
||||
List<Widget> createTestMenus({
|
||||
void Function(TestMenu)? onPressed,
|
||||
void Function(TestMenu, bool?)? onCheckboxChanged,
|
||||
void Function(TestMenu?)? onRadioChanged,
|
||||
void Function(TestMenu)? onOpen,
|
||||
void Function(TestMenu)? onClose,
|
||||
Map<TestMenu, MenuSerializableShortcut> shortcuts = const <TestMenu, MenuSerializableShortcut>{},
|
||||
bool? checkboxValue,
|
||||
TestMenu? radioValue,
|
||||
MenuController? menuController,
|
||||
TextEditingController? textEditingController,
|
||||
bool includeExtraGroups = false,
|
||||
bool accelerators = false,
|
||||
}) {
|
||||
Widget submenuButton(
|
||||
TestMenu menu, {
|
||||
required List<Widget> menuChildren,
|
||||
}) {
|
||||
return SubmenuButton(
|
||||
onOpen: onOpen != null ? () => onOpen(menu) : null,
|
||||
onClose: onClose != null ? () => onClose(menu) : null,
|
||||
menuChildren: menuChildren,
|
||||
child: accelerators ? MenuAcceleratorLabel(menu.acceleratorLabel) : Text(menu.label),
|
||||
);
|
||||
}
|
||||
|
||||
Widget menuItemButton(
|
||||
TestMenu menu, {
|
||||
bool enabled = true,
|
||||
Widget? leadingIcon,
|
||||
Widget? trailingIcon,
|
||||
Key? key,
|
||||
}) {
|
||||
return MenuItemButton(
|
||||
key: key,
|
||||
onPressed: enabled && onPressed != null ? () => onPressed(menu) : null,
|
||||
shortcut: shortcuts[menu],
|
||||
leadingIcon: leadingIcon,
|
||||
trailingIcon: trailingIcon,
|
||||
child: accelerators ? MenuAcceleratorLabel(menu.acceleratorLabel) : Text(menu.label),
|
||||
);
|
||||
}
|
||||
|
||||
Widget checkboxMenuButton(
|
||||
TestMenu menu, {
|
||||
bool enabled = true,
|
||||
bool tristate = false,
|
||||
Widget? leadingIcon,
|
||||
Widget? trailingIcon,
|
||||
Key? key,
|
||||
}) {
|
||||
return CheckboxMenuButton(
|
||||
key: key,
|
||||
value: checkboxValue,
|
||||
tristate: tristate,
|
||||
onChanged: enabled && onCheckboxChanged != null ? (bool? value) => onCheckboxChanged(menu, value) : null,
|
||||
shortcut: menu.shortcut,
|
||||
trailingIcon: trailingIcon,
|
||||
child: accelerators ? MenuAcceleratorLabel(menu.acceleratorLabel) : Text(menu.label),
|
||||
);
|
||||
}
|
||||
|
||||
Widget radioMenuButton(
|
||||
TestMenu menu, {
|
||||
bool enabled = true,
|
||||
bool toggleable = false,
|
||||
Widget? leadingIcon,
|
||||
Widget? trailingIcon,
|
||||
Key? key,
|
||||
}) {
|
||||
return RadioMenuButton<TestMenu>(
|
||||
key: key,
|
||||
groupValue: radioValue,
|
||||
value: menu,
|
||||
toggleable: toggleable,
|
||||
onChanged: enabled && onRadioChanged != null ? onRadioChanged : null,
|
||||
shortcut: menu.shortcut,
|
||||
trailingIcon: trailingIcon,
|
||||
child: accelerators ? MenuAcceleratorLabel(menu.acceleratorLabel) : Text(menu.label),
|
||||
);
|
||||
}
|
||||
|
||||
final List<Widget> result = <Widget>[
|
||||
submenuButton(
|
||||
TestMenu.mainMenu1,
|
||||
menuChildren: <Widget>[
|
||||
checkboxMenuButton(
|
||||
TestMenu.subMenu1,
|
||||
tristate: true,
|
||||
trailingIcon: const Icon(Icons.assessment),
|
||||
),
|
||||
radioMenuButton(
|
||||
TestMenu.radioMenu1,
|
||||
toggleable: true,
|
||||
trailingIcon: const Icon(Icons.assessment),
|
||||
),
|
||||
radioMenuButton(
|
||||
TestMenu.radioMenu2,
|
||||
toggleable: true,
|
||||
trailingIcon: const Icon(Icons.assessment),
|
||||
),
|
||||
radioMenuButton(
|
||||
TestMenu.radioMenu3,
|
||||
toggleable: true,
|
||||
trailingIcon: const Icon(Icons.assessment),
|
||||
),
|
||||
menuItemButton(
|
||||
TestMenu.subMenu2,
|
||||
leadingIcon: const Icon(Icons.send),
|
||||
trailingIcon: const Icon(Icons.mail),
|
||||
),
|
||||
],
|
||||
),
|
||||
submenuButton(
|
||||
TestMenu.mainMenu2,
|
||||
menuChildren: <Widget>[
|
||||
MenuAcceleratorCallbackBinding(
|
||||
onInvoke: onPressed != null
|
||||
? () {
|
||||
onPressed.call(TestMenu.testButton);
|
||||
menuController?.close();
|
||||
}
|
||||
: null,
|
||||
child: TextButton(
|
||||
onPressed: onPressed != null
|
||||
? () {
|
||||
onPressed.call(TestMenu.testButton);
|
||||
menuController?.close();
|
||||
}
|
||||
: null,
|
||||
child: accelerators
|
||||
? MenuAcceleratorLabel(TestMenu.testButton.acceleratorLabel)
|
||||
: Text(TestMenu.testButton.label),
|
||||
),
|
||||
),
|
||||
menuItemButton(TestMenu.subMenu3),
|
||||
],
|
||||
),
|
||||
submenuButton(
|
||||
TestMenu.mainMenu3,
|
||||
menuChildren: <Widget>[
|
||||
menuItemButton(TestMenu.subMenu8),
|
||||
],
|
||||
),
|
||||
submenuButton(
|
||||
TestMenu.mainMenu4,
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
onPressed: () {
|
||||
debugPrint('Activated text input item with ${textEditingController?.text} as a value.');
|
||||
},
|
||||
child: SizedBox(
|
||||
width: 200,
|
||||
child: TextField(
|
||||
controller: textEditingController,
|
||||
onSubmitted: (String value) {
|
||||
debugPrint('String $value submitted.');
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
submenuButton(
|
||||
TestMenu.subMenu5,
|
||||
menuChildren: <Widget>[
|
||||
menuItemButton(TestMenu.subSubMenu1),
|
||||
menuItemButton(TestMenu.subSubMenu2),
|
||||
if (includeExtraGroups)
|
||||
submenuButton(
|
||||
TestMenu.subSubMenu3,
|
||||
menuChildren: <Widget>[
|
||||
menuItemButton(TestMenu.subSubSubMenu1),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
menuItemButton(TestMenu.subMenu6, enabled: false),
|
||||
menuItemButton(TestMenu.subMenu7),
|
||||
menuItemButton(TestMenu.subMenu7),
|
||||
menuItemButton(TestMenu.subMenu8),
|
||||
],
|
||||
),
|
||||
];
|
||||
return result;
|
||||
}
|
||||
|
||||
enum TestMenu {
|
||||
mainMenu1('Menu 1'),
|
||||
mainMenu2('Menu 2'),
|
||||
mainMenu3('Menu 3'),
|
||||
mainMenu4('Menu 4'),
|
||||
mainMenu2('M&enu &2'),
|
||||
mainMenu3('Me&nu &3'),
|
||||
mainMenu4('Men&u &4'),
|
||||
radioMenu1('Radio Menu One', SingleActivator(LogicalKeyboardKey.digit1, control: true)),
|
||||
radioMenu2('Radio Menu Two', SingleActivator(LogicalKeyboardKey.digit2, control: true)),
|
||||
radioMenu3('Radio Menu Three', SingleActivator(LogicalKeyboardKey.digit3, control: true)),
|
||||
subMenu1('Sub Menu 1', SingleActivator(LogicalKeyboardKey.keyB, control: true)),
|
||||
subMenu2('Sub Menu 2'),
|
||||
subMenu3('Sub Menu 3', SingleActivator(LogicalKeyboardKey.enter, control: true)),
|
||||
subMenu4('Sub Menu 4'),
|
||||
subMenu5('Sub Menu 5'),
|
||||
subMenu6('Sub Menu 6', SingleActivator(LogicalKeyboardKey.tab, control: true)),
|
||||
subMenu7('Sub Menu 7'),
|
||||
subMenu8('Sub Menu 8'),
|
||||
subSubMenu1('Sub Sub Menu 1', SingleActivator(LogicalKeyboardKey.f10, control: true)),
|
||||
subSubMenu2('Sub Sub Menu 2'),
|
||||
subSubMenu3('Sub Sub Menu 3'),
|
||||
subSubSubMenu1('Sub Sub Sub Menu 1', SingleActivator(LogicalKeyboardKey.f11, control: true)),
|
||||
testButton('TEST button'),
|
||||
standaloneMenu1('Standalone Menu 1', SingleActivator(LogicalKeyboardKey.keyC, control: true)),
|
||||
standaloneMenu2('Standalone Menu 2');
|
||||
subMenu1('Sub Menu &1', SingleActivator(LogicalKeyboardKey.keyB, control: true)),
|
||||
subMenu2('Sub Menu &2'),
|
||||
subMenu3('Sub Menu &3', SingleActivator(LogicalKeyboardKey.enter, control: true)),
|
||||
subMenu4('Sub Menu &4'),
|
||||
subMenu5('Sub Menu &5'),
|
||||
subMenu6('Sub Menu &6', SingleActivator(LogicalKeyboardKey.tab, control: true)),
|
||||
subMenu7('Sub Menu &7'),
|
||||
subMenu8('Sub Menu &8'),
|
||||
subSubMenu1('Sub Sub Menu &1', SingleActivator(LogicalKeyboardKey.f10, control: true)),
|
||||
subSubMenu2('Sub Sub Menu &2'),
|
||||
subSubMenu3('Sub Sub Menu &3'),
|
||||
subSubSubMenu1('Sub Sub Sub Menu &1', SingleActivator(LogicalKeyboardKey.f11, control: true)),
|
||||
testButton('&TEST && &&& Button &'),
|
||||
standaloneMenu1('Standalone Menu &1', SingleActivator(LogicalKeyboardKey.keyC, control: true)),
|
||||
standaloneMenu2('Standalone Menu &2');
|
||||
|
||||
const TestMenu(this.label, [this.shortcut]);
|
||||
final String label;
|
||||
const TestMenu(this.acceleratorLabel, [this.shortcut]);
|
||||
final MenuSerializableShortcut? shortcut;
|
||||
final String acceleratorLabel;
|
||||
// Strip the accelerator markers.
|
||||
String get label => MenuAcceleratorLabel.stripAcceleratorMarkers(acceleratorLabel);
|
||||
int get acceleratorIndex {
|
||||
int index = -1;
|
||||
MenuAcceleratorLabel.stripAcceleratorMarkers(acceleratorLabel, setIndex: (int i) => index = i);
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user