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

atreeon_get_child_size 封面
套件詳情v0.1.1+1

atreeon_get_child_size

一個傳回子物件大小的組件。每次組件大小變更時,[onChange] 回調都會執行。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
12
喜歡數
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.

所屬分類

原始碼儲存庫

使用此套件的專案