context_menus 패키지는 사용자가 오른쪽 클릭 또는 긴 누름 제스처를 수행할 때 Flutter 앱에서 컨텍스트 메뉴를 표시할 수 있도록 해줍니다. 모바일, 웹, 데스크톱을 포함한 여러 플랫폼에서 일관되고 사용자 정의 가능한 방식으로 메뉴 옵션을 제공합니다. Flutter 위젯 시스템과 원활하게 통합되며 터치 및 포인터 기반 상호작용을 지원합니다. 사용이 간편하도록 설계되어 복잡한 사용자 정의 로직 없이도 빠르게 맥락 기반 작업을 구현할 수 있습니다. 맥락에 따라 메뉴를 통해 직관적인 사용자 인터랙션을 필요로 하는 애플리케이션에 적합합니다.
사용 사례
데스크톱에서 마우스 오른쪽 버튼 클릭 컨텍스트 메뉴
모바일에서 긴 누르기 메뉴
플랫폼 일관성 있는 UI 상호작용
사용자 정의 가능한 작업 패널
개선된 사용자 워크플로우
인터랙티브 앱 요소
주요 기능
크로스 플랫폼 지원
오른쪽 클릭하고 길게 누르면 트리거됨
사용자 정의 메뉴 항목
간단한 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:
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.