搜尋套件、儲存庫與 APP 分類

套件詳情v0.0.2

defer_pointer

一種取代 Overlay 的方法,可輕鬆在父級邊界之外渲染和命中測試小部件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
8萬
喜歡數
158
Pub 分數
130

安裝套件

flutter pub add defer_pointer

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

defer_pointer 套件提供了一種替代 Flutter 的 Overlay 的方法,讓元件能夠在其父元件邊界之外進行渲染與互動。它簡化了在 UI 層級結構中動態定位元件的命中測試,非常適合複雜的覆蓋層、工具提示或需要回應指標事件的浮動元件,而不受佈局限制。此套件由 gskinner.com 開發,支援所有主要平台,包括 Android、iOS、Web、macOS、Windows 與 Linux。該套件提升使用者互動的彈性,無需手動管理圖層。

適用情境

  • 建立浮動提示
  • 構建動態疊加層
  • 在受限佈局中實現拖放功能
  • 渲染超出父級邊界的互動元素
  • 設計自定義指針敏感的UI組件

主要特色

  • 支援跨邊界命中測試
  • 簡化覆蓋層實作
  • 適用於所有 Flutter 平台
  • 所需的樣板程式碼最少
  • 可無縫整合至現有的 widget 樹中

適合對象

  • 開發複雜 UI 互動的開發者
  • 需要響應式覆蓋層的團隊
  • 具有動態浮動元素的應用程式
  • 需要高階指針處理的UI庫
  • 針對多個平台的專案

使用建議

  • 可能影響大量活躍 widget 的效能
  • 需要仔細管理狀態
  • 不適合用於簡單的靜態覆蓋層
  • 除了基本用法之外,文件內容有限
  • 若未妥善管理,可能產生意外的指標捕捉風險

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

An alternative to Overlay which allows you to easily render and hit test a widget outside its parent bounds. Based on the original idea by @shrouxm here: https://github.com/flutter/flutter/issues/75747#issuecomment-907755810

Typically in Flutter, if you offset a widget outside of it's parent bounds hit-testing will break. DeferPointer works around this issue by handing off hit-testing and (optionally) rendering to an DeferredPointerHandler widget further up the tree.

While Overlay can solve this issue to some degree, using DeferPointer offers some benefits:

  • just works: no error prone and tedious layer management
  • more granular: you can set the bounds to be any ancestor widget you choose
  • more flexible: you can choose to paint the child on top, or not
  • easier to align/pin to a widget in the tree as that is the default expectation

This is useful for larger UI components like dropdown menus and sliding panels, as well as just small general styling tweaks.

🔨 Installation

dependencies:
  defer_pointer: ^0.0.2
⚙ Import
import 'package:defer_pointer/defer_pointer.dart';

🕹️ Usage

  1. Wrap a DeferredPointerHandler somewhere above the buttons that you wish to hit-test.
  2. Wrap DeferPointer around the buttons themselves.
Widget build(BuildContext context) {
    return DeferredPointerHandler(
       child: SizedBox(
           width: 100,
           height: 100,
           child: Stack(clipBehavior: Clip.none, children: [
             // Hang button off the bottom of the content
             Positioned(
               bottom: -30,
               child: DeferPointer(child: _SomeBtn(false)),
             ),
             // Content
             Positioned.fill(
               child: Container(
                 decoration: BoxDecoration(color: Colors.green, boxShadow: [
                   BoxShadow(color: Colors.black.withOpacity(1), blurRadius: 4, spreadRadius: 4),
                 ]),
               ),
             ),
           ]))));
  }

Enable paintOnTop if you need the child Widget painted on top of it's siblings. This will defer painting to the currently linked DeferredPointerHandler.

return DeferPointer(
    paintOnTop: true,
    child: TextButton(...));
Examples

There are 4 examples in this repo:

  1. A simple example of offsetting 2 buttons outside their stack: https://github.com/gskinnerTeam/flutter-defer-pointer/blob/master/example/lib/examples/simple_offset_outside_parent.dart

  2. A classic desktop/web style dropdown menu: https://github.com/gskinnerTeam/flutter-defer-pointer/blob/master/example/lib/examples/dropdown_menus.dart

  3. A animated menu based on the Flow widget: https://github.com/gskinnerTeam/flutter-defer-pointer/blob/master/example/lib/examples/flow_menu.dart

  4. A auto-complete search field: https://github.com/gskinnerTeam/flutter-defer-pointer/blob/master/example/lib/examples/auto_complete.dart

Manual Linking

By default a DeferPointer widget will look up the closest DeferredPointerHandler using it's current context. For more complicated use cases you can manually assign a link to bind a pointer to a handler:

final _deferredPointerLink = DeferredPointerHandlerLink();
...
Widget build(){
    return DeferredPointerHandler(
      link: _deferredPointerLink,
      child: Padding(
          padding: const EdgeInsets.all(20),
          child: DeferPointer(
            link: _deferredPointerLink,
            child: ...,
          )),
    );
}

🐞 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 項

原始碼儲存庫

使用此套件的專案