Search code examples
fluttermenuwidget

How to disable PlatfromMenuItem in Flutter


How to make a PlatformMenuItem disabled. Seems like there is no "enabled" property to control it.

PlatformMenuItem(
  label: 'Undo',
  shortcut:
  const SingleActivator(LogicalKeyboardKey.keyZ, meta: true),
  onSelected: () { ...},
)

Solution

  • According to the documentation, if you do not set the onSelected or onSelectedIntent fields of the PlatformMenuItem, it will be disabled. So, you can simply omit these fields when creating the menu item, like this:

    PlatformMenuItem(
      label: 'Undo',
      shortcut:
      const SingleActivator(LogicalKeyboardKey.keyZ, meta: true),
      // no onSelected or onSelectedIntent
    )
    

    Alternatively, you can set these fields to null, like this:

    PlatformMenuItem(
      label: 'Undo',
      shortcut:
      const SingleActivator(LogicalKeyboardKey.keyZ, meta: true),
      onSelected: null,
      onSelectedIntent: null,
    )