搜索插件、代码仓与 APP 分类

插件详情v2.0.0+1

context_menus

一个在右键单击或长按操作时显示上下文菜单的包。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2970
喜欢数
156
Pub 分数
135

安装插件

flutter pub add context_menus

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

context_menus 包使开发者能够在 Flutter 应用中,当用户执行右键单击或长按手势时显示上下文菜单。它提供了一种一致且可自定义的方式,在多个平台(包括移动设备、网页和桌面)上呈现菜单选项。该包与 Flutter 的小部件系统无缝集成,并支持触摸和指针交互。设计简洁易用,无需复杂的自定义逻辑即可快速实现上下文操作。适用于需要通过上下文敏感菜单实现直观用户交互的应用程序。

适合场景

  • 右键单击桌面的上下文菜单
  • 长按菜单在移动设备上
  • 平台一致的用户界面交互
  • 可自定义的操作面板
  • 增强的用户工作流
  • 交互式应用元素

主要特点

  • 跨平台支持
  • 右键单击并长按触发
  • 可自定义菜单项
  • 简单 API 集成
  • 响应式设计兼容性
  • 内置平台样式

适合对象

  • 桌面和移动应用开发者
  • UI密集型应用
  • 上下文操作界面
  • 需要手势菜单的应用程序
  • 寻求轻量级解决方案的开发者

使用建议

  • 需要手动处理指针事件
  • 有限的内置动画
  • 可能需要额外的样式以保持一致性
  • 不适合复杂的嵌套菜单
  • 依赖于正确的手势检测设置

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

context_menus

A package to show context menus on right-click or long-press.

🔨 Installation

dependencies:
  context_menus: ^1.0.2
⚙ Import
import 'package:context_menus/context_menus.dart';

🕹️ Usage

To get started, wrap a ContextMenuOverlay around your top-most view or app:

return ContextMenuOverlay(
  child: MaterialApp(...)
);

You can then use the ContextMenuRegion widget to tag sections of the Widget tree that should trigger a context menu.

/// Example hyperlink menu
return ContextMenuRegion(
  contextMenu: LinkContextMenu(url: 'http://flutter.dev'),
  child: TextButton(onPressed: () {}, child: Text("http://flutter.dev")),
),

Each ContextMenuRegion requires a contextMenu widget which will be shown on right-click or long-press.

Included in this package are a few pre-made ones which you can use as templates to quickly build your own.

  • LinkContextMenu
  • TextContextMenu
  • GenericContextMenu

✨ ️Styling

You have three options to modify styling

  • pass in custom ContextMenuButtonStyle for small styling tweaks
  • use your own cardBuilder or buttonBuilder delegate for more control
  • pass your own custom menus for total control

For basic styling, just pass button styling values to the ContextMenuOverlay:

return ContextMenuOverlay(
    buttonStyle: ContextMenuButtonStyle(
      fgColor: Colors.green,
      bgColor: Colors.red.shade100,
      hoverBgColor: Colors.red.shade200,
    ),
    child: MaterialApp(...);
}

For more control, you can overide the cardBuilder and buttonBuilder delegates:

return ContextMenuOverlay(
  /// Make a custom background
  cardBuilder: (_, children) => Container(color: Colors.purple.shade100, child: Column(children: children)),
  /// Make custom buttons
  buttonBuilder: (_, config, [__]) => TextButton(
    onPressed: config.onPressed,
    child: Container(width: double.infinity, child: Text(config.label)),
  ),
  child: MaterialApp( ... ),
);

Finally, you can disregard all of this and just provide your own menus directly to the ContextMenuRegion:

ContextMenuRegion(
  contextMenu: MyCustomMenu(),
  child: ...,
),

In this case, you are responsible for closing the menu when your buttons are triggered, which you can do using an extension on BuildContext:

context.contextMenuOverlay.hide();

💡 Custom Menus

The easiest way to create a custom menu is to use the GenericContextMenu. Just pass it a list of ContextMenuButtonConfig instances:

/// Custom Context Menu for an Image
ContextMenuRegion(
  contextMenu: GenericContextMenu(
    buttonConfigs: [
      ContextMenuButtonConfig(
        "View image in browser",
        onPressed: () => launch(_testImageUrl),
      ),
      ContextMenuButtonConfig(
        "Copy image path",
        onPressed: () => Clipboard.setData(ClipboardData(text: _testImageUrl)),
      )
    ],
  ),
  child: Image.network(_testImageUrl),
),

Another easy way to create custom menus is to use the ContextMenuStateMixin, and then call the cardBuilder and buttonBuilder methods to create your commands.

You can see this in action with the existing LinkContextMenu:

class _LinkContextMenuState extends State<LinkContextMenu> with ContextMenuStateMixin {
  @override
  Widget build(BuildContext context) {
    // cardBuilder is provided to us by the mixin, pass it a list of children to layout
    return cardBuilder.call(
      context,
      [
        // buttonBuilder is also provided by the mixin, use it to build each btn
        buttonBuilder.call(
          context,
          // button builder needs a config, so it knows how to setup the btn
          ContextMenuButtonConfig(
            "Open link in new window",
            icon: widget.useIcons ? Icon(Icons.link, size: 18) : null,
            onPressed: () => handlePressed(context, _handleNewWindowPressed),
          ),
        ),
        buttonBuilder.call(
          context,
          ContextMenuButtonConfig(
            "Copy link address",
            icon: widget.useIcons ? Icon(Icons.copy, size: 18) : null,
            onPressed: () => handlePressed(context, _handleClipboardPressed),
          ),
        )
      ],
    );
  }

In the above example, you could provide your own Card and Buttons directly, rather than using the builders, but the builders give you a couple of advantages:

  • You can style all menus using the button and card builders on the ContextMenuOverlay
  • All buttons will auto-close the context menu when triggered, saving you some boilerplate
  • Your custom menus will match the existing set of menus

🐞 Bugs/Requests

If you encounter any problems please open an issue. If you feel the library is missing a feature, please raise a ticket on Github and we'll look into it. Pull request are welcome.

📃 License

MIT License

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目