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

插件详情v0.5.0

value_layout_builder

带有额外值的 LayoutBuilder。当您希望在布局过程中计算一个值并构建 widget 时,它非常有用。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
31.3万
喜欢数
20
Pub 分数
150

安装插件

flutter pub add value_layout_builder

插件介绍

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

一个 LayoutBuilder 扩展,可在布局过程中提供额外的值。它使小部件能够动态响应布局约束,同时访问从这些约束中计算得出的值。这对于需要同时考虑尺寸和自定义逻辑的响应式设计特别有用。该包通过将布局感知与状态化计算相结合,增强了灵活性,使其非常适合依赖于尺寸和计算数据的复杂 UI 组件。

适合场景

  • 使用计算值的响应式布局
  • 基于约束的动态小部件大小调整
  • 需要布局感知逻辑的复杂UI组件
  • 与布局更改相关的自定义动画
  • 使用内部逻辑构建可重用的布局小部件

主要特点

  • 将 LayoutBuilder 扩展为带有额外值
  • 支持在布局过程中进行动态值计算
  • 通过上下文感知逻辑实现响应式设计
  • 最小化样板代码
  • 适用于所有 Flutter 平台

适合对象

  • 开发自适应用户界面的开发者
  • 需要布局上下文的 UI 组件创建者
  • 需要响应式和动态布局的项目
  • 使用功能或声明式 UI 模式的团队

使用建议

  • 需要理解 Flutter 的布局阶段
  • 可能为简单布局增加复杂性
  • 不适合静态或非响应式设计
  • 如果值计算很重,性能影响
  • 仅限在布局构建上下文中使用

原始 README

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

value_layout_builder

Pub

A LayoutBuilder with an extra value. It's useful when you want to build a widget with a value computed during layout.

Features

  • ValueLayoutBuilder - The RenderBox version.
  • SliverValueLayoutBuilder - The RenderSliver version.

Getting started

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  value_layout_builder:

In your library add the following import:

import 'package:value_layout_builder/value_layout_builder.dart';

For help getting started with Flutter, view the online documentation.

Usage

ValueLayoutBuilder and SliverValueLayoutBuilder will pair with a custom RenderObject which accepts a one of them as a child.

For example it is used in flutter_sticky_header in order to know how much the header is hidden.

The typical usage is to expose a builder in the parameter of your widget's constructor which takes a BuildContext and the value you want to get from layout, and to pass it from your custom RenderObject.

The following code is a dumb example to show you how to use it:

class _Tester<T> extends SingleChildRenderObjectWidget {
  const _Tester({
    Key? key,
    required this.value,
    required Widget child,
  }) : super(key: key, child: child);

  final T value;

  @override
  _RenderTester<T> createRenderObject(BuildContext context) {
    return _RenderTester<T>(value: value);
  }

  @override
  void updateRenderObject(
    BuildContext context,
    _RenderTester<T> renderObject,
  ) {
    renderObject.value = value;
  }
}

class _RenderTester<T> extends RenderBox
    with RenderObjectWithChildMixin<RenderBox> {
  _RenderTester({
    required T value,
  }) : _value = value;

  T get value => _value;
  T _value;
  set value(T value) {
    if (_value != value) {
      _value = value;
      markNeedsLayout();
    }
  }

  @override
  void performLayout() {
    // We create a specific constraints with the value we want to pass to the builder.
    BoxValueConstraints<T> constraints = BoxValueConstraints<T>(
      constraints: this.constraints,
      value: value,
    );
    child.layout(constraints);
    size = constraints.biggest;
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    context.paintChild(child, offset);
  }
}

We can then use _Tester<T> like this:

_Tester<String>(
  value: 'test',
  child: ValueLayoutBuilder<String>(
    builder: (context, constraints) {
      // The constraints holds the String value.
      final String value = constraints.value;
      return Text(value);
    },
  ),
)

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. By doing so, I will prioritize your issues or your pull-requests before the others.

Changelog

Please see the Changelog page to know what's recently changed.

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.

所属分类

源码仓库

使用此插件的项目