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

套件詳情v1.1.1

nil

一個簡單的 widget,當您想要顯示空白內容時可加入 widget 樹中,對效能影響最小。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
9530
喜歡數
324
Pub 分數
140

安裝套件

flutter pub add nil

套件介紹

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

nil 包提供了一個極簡且效能優化的 widget,當包含在 widget 樹中時,它不會渲染任何內容。它適用於需要佔位符或條件 widget 的情境,這些 widget 不會增加佈局或渲染開銷。與 SizedBox.shrink() 或 Container() 不同,nil 對 widget 樹的效能沒有任何影響,非常適合在外部處理可見性條件的動態 UI 邏輯中使用。它輕量且安全,適用於複雜或經常更新的 UI。

適用情境

  • 條件渲染且無視覺輸出
  • 在動態佈局中替換空容器
  • 在大型小部件樹中優化性能
  • 簡化小部件組合中的空值檢查
  • 避免不必要的佈局計算

主要特色

  • 零渲染成本
  • 無佈局影響
  • 最小記憶體佔用
  • 簡單 API
  • 適合頻繁更新

適合對象

  • 開發者優化UI效能
  • 動態或條件UI的建構者
  • 優先考慮最小開銷的團隊
  • 具有複雜或嵌套小部件樹的應用程式
  • 尋找空小部件的乾淨替代方案的人

使用建議

  • 不適合用於可見內容
  • 不支援子組件
  • 僅在有意不渲染任何內容時有用
  • 如果使用內建的空安全構造,則不需要
  • 最好與條件邏輯結合使用

原始 README

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

Build Pub Codecov

nil

A simple widget to add in the widget tree when you want to show nothing, with minimal impact on performance.

Why?

Sometimes, according to a condition, we want to display nothing. Usually when we can't return null, we would return something like const SizedBox() for example.

This is good, but it has some performance impacts since SizedBox creates a RenderObject. The RenderObject lives in the render tree and some computations are performed on it, even if it paints nothing on the screen.

We can do better, we can have a widget which does not create a RenderObject, while being still valid. The Nil widget is the minimal implementation for this use case. It only creates an Element and does nothing while it's building. Because the optimal way to use it, is to call const Nil(), it also comes with a nil constant that you can use everywhere (which is a const Nil()).

Usage

import 'package:nil/nil.dart';

return Builder(
  builder: (_) {
    if (condition) {
      return const MyWidget();
    } else {
      return nil;
    }
  },
);

Warning

This widget is not intended to be used in widgets accepting multiple children (e.g. Rows, Columns, etc.). The best option is to not add a widget in the list if you don't want it to be displayed. Moreover using a Nil widget in such a case, can have unexpected results.

Sponsoring

I'm working on my packages on my free-time, but I don't have as much time as I would. If this package or any other package I created is helping you, please consider to sponsor me so that I can take time to read the issues, fix bugs, merge pull requests and add features to these packages.

Contributions

Feel free to contribute to this project.

If you find a bug or want a feature, but don't know how to fix/implement it, please fill an issue.
If you fixed a bug or implemented a feature, please send a pull request.

所屬分類

原始碼儲存庫

使用此套件的專案