Flutter Desktop Apps: Getting Started

Mar 28 2023 · Dart 2.19, Flutter 3.7, Android Studio 2021.3.1 or higher, Visual Studo Code 1.7.4 or higher

Part 1: Flutter Desktop Apps

06. System Menu Coding: Part 1

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 05. Create System Menus Next episode: 07. System Menu Coding: Part 2

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 06. System Menu Coding: Part 1

Android Studio

Now open up menus.dart. You will see a lot of empty methods. RiverPod is used to create the MenuProvider. This allows menus to access any other provider via the ref variable. There are two main methods: createMenus for the Mac and createWindowsMenus for Windows. Here you will build up your main menu system. You want a File, Edit, Todos, Find and Help menus. Most of these will just be empty menus that you can add functionality to later.

Start by adding the Mac menu items in the createMenus method:

return [
  createFileMenu(),
  createEditMenu(),
];
}

The menubar plugin has a function called setApplicationMenu that will set the main menu for Windows from the given list of menus. Add the following to the createWindowsMenu method:

setApplicationMenu([
  createWindowsFileMenu(),
  createWindowsEditMenu(),
]);

This will create the 5 menus we want to show. These haven’t been created yet. Let’s start with the file menu. Add:

PlatformMenu createFileMenu() {
  return PlatformMenu(label: 'File', menus: [
    PlatformMenuItem(label: 'Import', onSelected: () => handleImport()),
    PlatformMenuItem(label: 'Export', onSelected: () => handleExport())
      PlatformMenuItemGroup(members: [
        PlatformMenuItem(
            label: 'Quit',
            onSelected: () => handleQuit(),
            shortcut: const SingleActivator(
                LogicalKeyboardKey.keyQ, meta: true))
      ])
  ]);
}

Then create the Windows version:

NativeSubmenu createWindowsFileMenu() {
  return NativeSubmenu(label: 'File', children: [
    NativeMenuItem(label: 'Import', onSelected: () => handleImport()),
    NativeMenuItem(label: 'Export', onSelected: () => handleExport())
    NativeMenuItem(
        label: 'Quit',
        onSelected: () => handleQuit(),
        shortcut:
            LogicalKeySet(LogicalKeyboardKey.meta, LogicalKeyboardKey.keyQ)),
  ]);
}

This method returns a submenu with a name of file and two children that are MenuItems: Import and export. The NativeSubmenu and the NativeMenuItem classes are from the plugin. When clicked, these methods will call the handleImport and handleExport methods respectively.

Next, create the edit menu. Add:

PlatformMenu createEditMenu() {
  return PlatformMenu(label: 'Edit', menus: [
    PlatformMenuItem(label: 'Cut', onSelected: () => handleCut()),
    PlatformMenuItem(label: 'Copy', onSelected: () => handleCopy()),
    PlatformMenuItem(label: 'Paste', onSelected: () => handlePaste())
  ]);
}

Now the Windows version:

NativeSubmenu createWindowsEditMenu() {
  return NativeSubmenu(label: 'Edit', children: [
    NativeMenuItem(label: 'Cut', onSelected: () => handleCut()),
    NativeMenuItem(label: 'Copy', onSelected: () => handleCopy()),
    NativeMenuItem(label: 'Paste', onSelected: () => handlePaste())
  ]);
}

This will create a standard edit menu with cut, copy and paste.

Stop and rerun the app and make sure all of these menus exist.

In the next episode you will add the last three menus.