패키지, 저장소 및 앱 카테고리 검색

패키지 상세v2.0.0+1

context_menus

오른쪽 클릭 또는 긴 누름 시 컨텍스트 메뉴를 표시하는 패키지입니다.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
3천
좋아요
156
Pub 점수
135

패키지 설치

flutter pub add context_menus

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

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:

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 개

소스 저장소

이 패키지를 사용하는 프로젝트