CheckboxMenuButton class

A menu item that combines a Checkbox widget with a MenuItemButton.

To style the checkbox separately from the button, add a CheckboxTheme ancestor.

This example shows a menu with a checkbox that shows a message in the body of the app if checked.

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 [CheckboxMenuButton].

void main() => runApp(const MenuApp());

class MyCheckboxMenu extends StatefulWidget {
  const MyCheckboxMenu({super.key, required this.message});

  final String message;

  @override
  State<MyCheckboxMenu> createState() => _MyCheckboxMenuState();
}

class _MyCheckboxMenuState extends State<MyCheckboxMenu> {
  final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Menu Button');
  static const SingleActivator _showShortcut = SingleActivator(
    LogicalKeyboardKey.keyS,
    control: true,
  );
  bool _showingMessage = false;

  @override
  void dispose() {
    _buttonFocusNode.dispose();
    super.dispose();
  }

  void _setMessageVisibility(bool visible) {
    setState(() {
      _showingMessage = visible;
    });
  }

  @override
  Widget build(BuildContext context) {
    return CallbackShortcuts(
      bindings: <ShortcutActivator, VoidCallback>{
        _showShortcut: () {
          _setMessageVisibility(!_showingMessage);
        },
      },
      child: Column(
        crossAxisAlignment: .start,
        children: <Widget>[
          MenuAnchor(
            childFocusNode: _buttonFocusNode,
            menuChildren: <Widget>[
              CheckboxMenuButton(
                value: _showingMessage,
                onChanged: (bool? value) {
                  _setMessageVisibility(value!);
                },
                child: const Text('Show Message'),
              ),
            ],
            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: .center,
              child: Column(
                mainAxisAlignment: .center,
                children: <Widget>[
                  Padding(
                    padding: const .all(12.0),
                    child: Text(
                      _showingMessage ? widget.message : '',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

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: SafeArea(child: MyCheckboxMenu(message: kMessage)),
      ),
    );
  }
}

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

Constructors

CheckboxMenuButton({Key? key, required bool? value, bool tristate = false, bool isError = false, required ValueChanged<bool?>? onChanged, 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 CheckboxMenuButton.
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
hashCode → int
The hash code for this object.
no setterinherited
isError → bool
True if this checkbox wants to show an error state.
final
key → Key?
Controls how one widget replaces another widget in the tree.
finalinherited
onChanged → ValueChanged<bool?>?
Called when the value of the checkbox should change.
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
trailingIcon → Widget?
An optional icon to display after the child label.
final
tristate → bool
If true, then the checkbox's value can be true, false, or null.
final
value → bool?
Whether this checkbox is checked.
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