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

插件详情v1.0.0

nested

一个帮助嵌套多个小部件而无需手动嵌套的 Flutter 小部件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
520万
喜欢数
350
Pub 分数
150

安装插件

flutter pub add nested

插件介绍

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

嵌套包是 Flutter 的一个实用工具,通过提供一种简洁、声明式的方法来组合多个 widget,从而简化了多层 widget 的嵌套。它允许开发者以更直观的方式链式排列 widget,增强了复杂 UI 布局的可读性和可维护性。非常适合减少 UI 代码中的样板文件,支持所有主要的 Flutter 平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。

适合场景

  • 简化复杂的部件层次结构
  • 减少嵌套小部件样板代码
  • 提高UI代码可读性
  • 简化布局组合
  • 创建可重用的部件链
  • 增强大型UI的可维护性

主要特点

  • 声明式组件嵌套
  • 最小化样板代码
  • 可链式组合的 Widget
  • 跨平台支持
  • 简洁、可读性高的语法
  • 无额外依赖

适合对象

  • 寻求更简洁 UI 代码的开发者
  • 具有深层嵌套 Widget 的项目
  • 注重代码可维护性的团队
  • UI 密集型应用
  • Flutter 初学者学习组合
  • 经验丰富的开发人员优化布局模式

使用建议

  • 仅限 widget 组合
  • 不是高级布局 widget 的替代品
  • 可能增加新用户的上手难度
  • 最好与其他布局策略结合使用
  • 不替代状态管理
  • 需要理解 Flutter 的 widget 树

原始 README

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

pub package ci

A widget that simplifies the syntax for deeply nested widget trees.

Motivation

Widgets tend to get pretty nested rapidly. It's not rare to see:

MyWidget(
  child: AnotherWidget(
    child: Again(
      child: AndAgain(
        child: Leaf(),
      )
    )
  )
)

That's not very ideal.

There's where nested propose a solution. Using nested, it is possible to flatten the previous tree into:

Nested(
  children: [
    MyWidget(),
    AnotherWidget(),
    Again(),
    AndAgain(),
  ],
  child: Leaf(),
),

That's a lot more readable!

Usage

Nested relies on a new kind of widget: SingleChildWidget, which has two concrete implementation:

These are SingleChildWidget variants of the original Stateless/StatefulWidget.

The difference between a widget and its single-child variant is that they have a custom build method that takes an extra parameter.

As such, a StatelessWidget would be:

class MyWidget extends StatelessWidget {
  MyWidget({Key key, this.child}): super(key: key);

  final Widget child;

  @override
  Widget build(BuildContext context) {
    return SomethingWidget(child: child);
  }
}

Whereas a SingleChildStatelessWidget would be:

class MyWidget extends SingleChildStatelessWidget {
  MyWidget({Key key, Widget child}): super(key: key, child: child);

  @override
  Widget buildWithChild(BuildContext context, Widget child) {
    return SomethingWidget(child: child);
  }
}

This allows our new MyWidget to be used both with:

MyWidget(
  child: AnotherWidget(),
)

and to be placed inside children of [Nested] like so:

Nested(
  children: [
    MyWidget(),
    ...
  ],
  child: AnotherWidget(),
)

所属分类

源码仓库

使用此插件的项目