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

atreeon_get_child_size 封面
插件详情v0.1.1+1

atreeon_get_child_size

一个返回子对象大小的控件。每当控件大小发生变化时,[onChange] 回调都会执行。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
8
喜欢数
2
Pub 分数
160

安装插件

flutter pub add atreeon_get_child_size

插件介绍

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

atreeon_get_child_size 包提供了一个 Flutter 小部件,用于测量并公开其子小部件的大小。当子小部件的尺寸发生变化时,该组件会触发 onChange 回调,从而实现对布局更新的动态响应。适用于需要实时跟踪大小的场景,例如基于容器尺寸的响应式布局或动画。该小部件轻量且可无缝集成到现有 Flutter 应用中,支持所有主要平台。

适合场景

  • 响应式UI调整
  • 动态动画触发
  • 布局调试
  • 实时大小监控
  • 自定义布局计算

主要特点

  • 实时大小更新
  • OnChange 回调支持
  • 跨平台兼容性
  • 最小性能开销
  • 与现有小部件的简单集成

适合对象

  • 需要尺寸反馈的开发者
  • 需要动态大小的UI组件
  • 调试布局问题
  • 基于尺寸的动画
  • 跨平台应用开发

使用建议

  • 在敏感上下文中使用可能导致重建
  • 在复杂树结构中需要谨慎处理
  • 不适用于静态尺寸需求
  • 大型 widget 树中的潜在性能影响
  • 仅限直接子项大小测量

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

atreeon_get_child_size

A widget that returns the size of the child object. The [onChange] callback will run everytime the widget's size has changed.

atreeon_get_child_size example

Waring! will result in multiple passes from the layout algorithm if you rebuild a widget from onChange; it will affect performance. This can happen even when just writing out to text the screen size. For example if you have more go from a '10' in the output to a '100', that can affect the size of a widget. Use with care.

This is a fundamental restriction in how widgets are built -> constraints go down & sizes go up -> if the constraints change the size could change.

Example

class _GetChildSize_DemoState extends State<GetChildSize_Demo> {
  void initState() {
    super.initState();
  }

  Size myChildSize = Size(10, 10);

  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(myChildSize.height.toString()),
        Expanded(
          child: GetChildSize(
            onChange: (size) {
              setState(() {
                myChildSize = size;
              });
            },
            child: Text(myChildSize.height.toString()),
          ),
        )
      ],
    );
  }
}

Features

  • returns the size of the child object and runs a callback
Other Info
Discussion on code & implementation

https://github.com/flutter/flutter/issues/14488

If you just want to know the dimensions between layouts (i.e. you're not using them to affect the layout), you can just read them right out of the rendering tree (get the RenderObject from the BuildContext, etc).

However, if you are using the dimensions to actually affect the layout, then that will lead to horrible effects like lagging one frame behind, or worse. In those cases, you want to just build your own render object, or use a layout builder, or similar, depending on exactly what your use case is.

I need the size of a child to use it in the parent's build.

Fundamentally that's not possible, because at the time of the parent's build the child hasn't laid out yet. If you want to get the child's size during the parent's layout, you can do that by creating your own RenderObject. Just look at how e.g. Align is implemented.

所属分类

源码仓库

使用此插件的项目