Package selection guidance compiled from public package information, community data, and project usage.
A LayoutBuilder extension that provides an additional value during the layout process. It enables widgets to react dynamically to layout constraints while also accessing a computed value derived from those constraints. This is particularly useful for responsive designs where both size and custom logic need to be considered simultaneously. The package enhances flexibility by combining layout awareness with stateful computation, making it ideal for complex UI components that depend on both dimensions and calculated data.
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> extendsSingleChildRenderObjectWidget{
const_Tester({
Key? key,
requiredthis.value,
requiredWidget child,
}) : super(key: key, child: child);
final T value;
@override_RenderTester<T> createRenderObject(BuildContext context) {
return_RenderTester<T>(value: value);
}
@overridevoidupdateRenderObject(
BuildContext context,
_RenderTester<T> renderObject,
) {
renderObject.value = value;
}
}
class_RenderTester<T> extendsRenderBoxwithRenderObjectWithChildMixin<RenderBox> {
_RenderTester({
required T value,
}) : _value = value;
T get value => _value;
T _value;
setvalue(T value) {
if (_value != value) {
_value = value;
markNeedsLayout();
}
}
@overridevoidperformLayout() {
// 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;
}
@overridevoidpaint(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.finalString value = constraints.value;
returnText(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.