Implements a PlatformMenuBar widget and associated data structures (#100274)
Implements a PlatformMenuBar widget and associated data structures for defining menu bars that use native APIs for rendering. This PR includes: A PlatformMenuBar class, which is a widget that menu bar data can be attached to for sending to the platform. A PlatformMenuDelegate base, which is the type taken by a new WidgetsBinding.platformMenuDelegate. An implementation of the above in DefaultPlatformMenuDelegate that talks to the built-in "flutter/menu" channel to talk to the built-in platform implementation. The delegate is so that a plugin could override with its own delegate and provide other platforms with native menu support using the same widgets to define the menus. This is the framework part of the implementation. The engine part will be in flutter/engine#32080 (and flutter/engine#32358)
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flutter code sample for PlatformMenuBar
|
||||
|
||||
////////////////////////////////////
|
||||
// THIS SAMPLE ONLY WORKS ON MACOS.
|
||||
////////////////////////////////////
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
void main() => runApp(const SampleApp());
|
||||
|
||||
enum MenuSelection {
|
||||
about,
|
||||
showMessage,
|
||||
}
|
||||
|
||||
class SampleApp extends StatelessWidget {
|
||||
const SampleApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: Scaffold(body: MyMenuBarApp()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyMenuBarApp extends StatefulWidget {
|
||||
const MyMenuBarApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<MyMenuBarApp> createState() => _MyMenuBarAppState();
|
||||
}
|
||||
|
||||
class _MyMenuBarAppState extends State<MyMenuBarApp> {
|
||||
String _message = 'Hello';
|
||||
bool _showMessage = false;
|
||||
|
||||
void _handleMenuSelection(MenuSelection value) {
|
||||
switch (value) {
|
||||
case MenuSelection.about:
|
||||
showAboutDialog(
|
||||
context: context,
|
||||
applicationName: 'MenuBar Sample',
|
||||
applicationVersion: '1.0.0',
|
||||
);
|
||||
break;
|
||||
case MenuSelection.showMessage:
|
||||
setState(() {
|
||||
_showMessage = !_showMessage;
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
////////////////////////////////////
|
||||
// THIS SAMPLE ONLY WORKS ON MACOS.
|
||||
////////////////////////////////////
|
||||
|
||||
// This builds a menu hierarchy that looks like this:
|
||||
// Flutter API Sample
|
||||
// ├ About
|
||||
// ├ ──────── (group divider)
|
||||
// ├ Hide/Show Message
|
||||
// ├ Messages
|
||||
// │ ├ I am not throwing away my shot.
|
||||
// │ └ There's a million things I haven't done, but just you wait.
|
||||
// └ Quit
|
||||
return PlatformMenuBar(
|
||||
menus: <MenuItem>[
|
||||
PlatformMenu(
|
||||
label: 'Flutter API Sample',
|
||||
menus: <MenuItem>[
|
||||
PlatformMenuItemGroup(
|
||||
members: <MenuItem>[
|
||||
PlatformMenuItem(
|
||||
label: 'About',
|
||||
onSelected: () {
|
||||
_handleMenuSelection(MenuSelection.about);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
PlatformMenuItemGroup(
|
||||
members: <MenuItem>[
|
||||
PlatformMenuItem(
|
||||
onSelected: () {
|
||||
_handleMenuSelection(MenuSelection.showMessage);
|
||||
},
|
||||
shortcut: const CharacterActivator('m'),
|
||||
label: _showMessage ? 'Hide Message' : 'Show Message',
|
||||
),
|
||||
PlatformMenu(
|
||||
label: 'Messages',
|
||||
menus: <MenuItem>[
|
||||
PlatformMenuItem(
|
||||
label: 'I am not throwing away my shot.',
|
||||
shortcut: const SingleActivator(LogicalKeyboardKey.digit1, meta: true),
|
||||
onSelected: () {
|
||||
setState(() {
|
||||
_message = 'I am not throwing away my shot.';
|
||||
});
|
||||
},
|
||||
),
|
||||
PlatformMenuItem(
|
||||
label: "There's a million things I haven't done, but just you wait.",
|
||||
shortcut: const SingleActivator(LogicalKeyboardKey.digit2, meta: true),
|
||||
onSelected: () {
|
||||
setState(() {
|
||||
_message = "There's a million things I haven't done, but just you wait.";
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
],
|
||||
),
|
||||
if (PlatformProvidedMenuItem.hasMenu(PlatformProvidedMenuItemType.quit))
|
||||
const PlatformProvidedMenuItem(type: PlatformProvidedMenuItemType.quit),
|
||||
],
|
||||
),
|
||||
],
|
||||
body: Center(
|
||||
child: Text(_showMessage
|
||||
? _message
|
||||
: 'This space intentionally left blank.\n'
|
||||
'Show a message here using the menu.'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user