搜索插件、代码仓与 APP 分类

measure_size 封面
插件详情v5.0.2

measure_size

一个在大小改变时触发回调的控件

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.5万
喜欢数
40
Pub 分数
160

安装插件

flutter pub add measure_size

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

measure_size 包是一个 Flutter 小部件,当其大小发生变化时会触发回调,从而根据实际渲染尺寸实现动态布局调整。它包装一个子小部件,并在构建和布局阶段监控其大小,非常适合用于响应式设计或依赖精确测量的动画。该包可在所有主要平台(包括 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

所属分类

源码仓库

使用此插件的项目