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

measure_size 封面
套件詳情v5.0.2

measure_size

一個在大小改變時觸發回調的組件

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1.5萬
喜歡數
40
Pub 分數
160

安裝套件

flutter pub add measure_size

套件介紹

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

measure_size 包是一個 Flutter widget,當其大小改變時會觸發回調,進而根據實際渲染尺寸實現動態佈局調整。它包裝一個子 widget,並在建構與佈局階段監控其大小,非常適合用於響應式設計或依賴精確測量的動畫。此套件可在所有主要平台(包含 Android、iOS、Web、macOS、Windows 與 Linux)上運作,確保行為一致。當需要對容器大小變更做出反應,而不需依賴固定值時,特別實用。

適用情境

  • 具有動態尺寸的響應式 UI
  • 基於元件尺寸的動畫
  • 佈局除錯
  • 動態內容渲染
  • 即時大小監控

主要特色

  • 在大小變更時觸發回呼
  • 跨平台相容性
  • 與現有組件的簡單整合
  • 支援所有 Flutter 平台
  • 無外部依賴

適合對象

  • 需要即時大小反饋的開發人員
  • 需要自適應佈局的UI組件
  • 測試和調試佈局問題
  • 具有動態或流體設計系統的專案

使用建議

  • 如果過度使用,可能會影響效能
  • 回調可能在佈局期間多次觸發
  • 不適合靜態尺寸需求
  • 在複雜的 widget 樹中需要謹慎處理
  • 僅限大小變更,不包含位置或偏移

原始 README

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

measure_size

A Flutter package for efficiently measuring widget sizes and building size-aware, adaptive layouts with minimal overhead.


Measuring text and drawing an overlay with matching size


MeasureSize MeasureSizeBuilder
Purpose Monitor size changes Prototype-driven layout
Callback timing Post-frame During layout (invokeLayoutCallback)
Measurements Single widget Multiple prototypes
Use case Reactive to size changes Proactive layout decisions
Complexity Simple Advanced
Performance Good Excellent (single pass)
PreferredSizeWidget limitation Does not work First frame problematic

import 'package:measure_size/measure_size.dart';

Widget child = Text('Lorem ipsum dolor sit amet');

MeasureSize(
    onChange: (Size newSize) {
        /// [newSize] will be the displayed size of [Widget child]
    },
    child: child,
);

MeasureSizeBuilder<String>(
    prototypeConstraints: (constraints) => BoxConstraints(
      minWidth: 0,
      maxWidth: constraints.maxWidth,
      minHeight: 0,
      maxHeight: double.infinity,
    ),
    prototypes: [
      PrototypeId(
        id: 'lorem',
        child: child,
      ),
    ],
    builder: (context, sizes) {
      // [sizes['lorem']] will be the layed out size of [Widget child]
      return child;
    }
);

MeasureSize

Required
  • Widget child - This widget will be displayed and and measured.
Optional
  • void onChange(Size newSize) - A callback that is fired exactly once after the first frame of the child widget is rendered.

MeasureSizeBuilder<T>

Required
  • List<PrototypeId<T>> prototypes - Wrap widgets you'd like to measure with PrototypeId and provide a unique identifier for each widget.
  • PrototypeWidgetBuilder<T> builder - A builder function that contains the sizes of all prototypes.
  • PrototypeConstraintCalculator prototypeConstraints - Constraints to be assigned to each prototype widget.

Example

A demonstration can be found in the example directory. To run the example:

flutter run example/main.dart

Credits

所屬分類

原始碼儲存庫

使用此套件的專案