搜索插件、代码仓与 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.

所属分类

源码仓库

使用此插件的项目