RadioMenuButton<T> class
A menu item that combines a Radio widget with a MenuItemButton.
To style the radio button separately from the overall button, add a RadioTheme ancestor.
This example shows a menu with three radio buttons with shortcuts that changes the background color of the body when the buttons are selected.
To see it in action, copy and run this code snippet on DartPad.
import 'package:flutter/services.dart';
import 'package:material_ui/material_ui.dart';
/// Flutter code sample for [RadioMenuButton].
void main() => runApp(const MenuApp());
class MyRadioMenu extends StatefulWidget {
const MyRadioMenu({super.key});
@override
State<MyRadioMenu> createState() => _MyRadioMenuState();
}
class _MyRadioMenuState extends State<MyRadioMenu> {
final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Menu Button');
Color _backgroundColor = Colors.red;
late ShortcutRegistryEntry _entry;
static const SingleActivator _redShortcut = SingleActivator(
LogicalKeyboardKey.keyR,
control: true,
);
static const SingleActivator _greenShortcut = SingleActivator(
LogicalKeyboardKey.keyG,
control: true,
);
static const SingleActivator _blueShortcut = SingleActivator(
LogicalKeyboardKey.keyB,
control: true,
);
@override
void didChangeDependencies() {
super.didChangeDependencies();
_entry = ShortcutRegistry.of(context).addAll(<
ShortcutActivator,
VoidCallbackIntent
>{
_redShortcut: VoidCallbackIntent(() => _setBackgroundColor(Colors.red)),
_greenShortcut: VoidCallbackIntent(
() => _setBackgroundColor(Colors.green),
),
_blueShortcut: VoidCallbackIntent(() => _setBackgroundColor(Colors.blue)),
});
}
@override
void dispose() {
_buttonFocusNode.dispose();
_entry.dispose();
super.dispose();
}
void _setBackgroundColor(Color? color) {
setState(() {
_backgroundColor = color!;
});
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: .start,
children: <Widget>[
MenuAnchor(
childFocusNode: _buttonFocusNode,
menuChildren: <Widget>[
RadioMenuButton<Color>(
value: Colors.red,
shortcut: _redShortcut,
groupValue: _backgroundColor,
onChanged: _setBackgroundColor,
child: const Text('Red Background'),
),
RadioMenuButton<Color>(
value: Colors.green,
shortcut: _greenShortcut,
groupValue: _backgroundColor,
onChanged: _setBackgroundColor,
child: const Text('Green Background'),
),
RadioMenuButton<Color>(
value: Colors.blue,
shortcut: _blueShortcut,
groupValue: _backgroundColor,
onChanged: _setBackgroundColor,
child: const Text('Blue Background'),
),
],
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(color: _backgroundColor)),
],
);
}
}
class MenuApp extends StatelessWidget {
const MenuApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(body: SafeArea(child: MyRadioMenu())),
);
}
}
See also:
- MenuBar, a widget that creates a menu bar of cascading menu items.
- MenuAnchor, a widget that defines a region which can host a cascading menu.
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatelessWidget
- RadioMenuButton
Constructors
-
RadioMenuButton({Key? key, required T value, required T? groupValue, required ValueChanged<
T?> ? onChanged, bool toggleable = false, ValueChanged<bool> ? onHover, ValueChanged<bool> ? onFocusChange, FocusNode? focusNode, MenuSerializableShortcut? shortcut, ButtonStyle? style, MaterialStatesController? statesController, Clip clipBehavior = Clip.none, Widget? trailingIcon, bool closeOnActivate = true, required Widget? child}) -
Creates a const RadioMenuButton.
const
Properties
- child → Widget?
-
The widget displayed in the center of this button.
final
- clipBehavior → Clip
-
The content will be clipped (or not) according to this option.
final
- closeOnActivate → bool
-
Determines if the menu will be closed when a MenuItemButton
is pressed.
final
- enabled → bool
-
Whether the button is enabled or disabled.
no setter
- focusNode → FocusNode?
-
An optional focus node to use as the focus node for this widget.
final
- groupValue → T?
-
The currently selected value for a group of radio buttons.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
-
onChanged
→ ValueChanged<
T?> ? -
Called when the user selects this radio button.
final
-
onFocusChange
→ ValueChanged<
bool> ? -
Handler called when the focus changes.
final
-
onHover
→ ValueChanged<
bool> ? -
Called when a pointer enters or exits the button response area.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- shortcut → MenuSerializableShortcut?
-
The optional shortcut that selects this MenuItemButton.
final
- statesController → MaterialStatesController?
-
Represents the interactive "state" of this widget in terms of
a set of WidgetStates, like WidgetState.pressed and
WidgetState.focused.
final
- style → ButtonStyle?
-
Customizes this button's appearance.
final
- toggleable → bool
-
Set to true if this radio button is allowed to be returned to an
indeterminate state by selecting it again when selected.
final
- trailingIcon → Widget?
-
An optional icon to display after the child label.
final
- value → T
-
The value represented by this radio button.
final
Methods
-
build(
BuildContext context) → Widget -
Describes the part of the user interface represented by this widget.
override
-
createElement(
) → StatelessElement -
Creates a StatelessElement to manage this widget's location in the tree.
inherited
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited