Package selection guidance compiled from public package information, community data, and project usage.
The context_menus package enables developers to display context menus in Flutter apps when users perform right-clicks or long-press gestures. It provides a consistent and customizable way to present menu options across multiple platforms, including mobile, web, and desktop. The package integrates seamlessly with Flutter's widget system and supports both touch and pointer-based interactions. Designed for ease of use, it allows quick implementation of contextual actions without requiring complex custom logic. Ideal for applications needing intuitive user interactions through context-sensitive menus.
Use cases
Right-click context menus on desktop
Long-press menus on mobile
Platform-consistent UI interactions
Customizable action panels
Enhanced user workflows
Interactive app elements
Key features
Cross-platform support
Right-click and long-press triggers
Customizable menu items
Simple API integration
Responsive design compatibility
Built-in platform styling
Suitable for
Desktop and mobile app developers
UI-heavy applications
Contextual action interfaces
Apps requiring gesture-based menus
Developers seeking lightweight solutions
Considerations
Requires manual handling of pointer events
Limited built-in animations
May need additional styling for consistency
Not suitable for complex nested menus
Depends on correct gesture detection setup
Original README
Original README snapshot from the linked source repository. Visit the repository for the latest content.
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:
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 ImageContextMenuRegion(
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_LinkContextMenuStateextendsState<LinkContextMenu> withContextMenuStateMixin{
@overrideWidgetbuild(BuildContext context) {
// cardBuilder is provided to us by the mixin, pass it a list of children to layoutreturn 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 btnContextMenuButtonConfig(
"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.