Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

docs.flutter.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more

Flutter 3.41 is live! Check out theFlutter 3.41 blog post!

Dry layout support for RenderBox

The method "computeDryLayout" was added to the RenderBox protocol to correctly calculate its intrinsic size in certain situations.

Important

Summary

#

A new method namedcomputeDryLayout was added to theRenderBox protocol. Subclasses ofRenderBox are expected to implement it to correctly report their desired size given a set ofBoxConstraints during intrinsic calculations. Subclasses that implementcomputeDryLayout no longer need to overrideperformResize.

Context

#

A new method,computeDryLayout, was added to theRenderBox protocol to correctly calculate the intrinsic sizes of aRenderParagraph withWidgetSpan children and aRenderWrap. The method receives a set ofBoxConstraints and is expected to calculate the resulting size of theRenderBox without changing any internal state. It's essentially a dry run ofperformLayout that only calculates the resulting size and doesn't place the children. ThecomputeDryLayout method is part of the intrinsics protocol (see alsoRenderBox.computeMinIntrinsicWidth and friends).

Description of change

#

Subclasses ofRenderBox need to override the newcomputeDryLayout method if they are used as a descendant of aRenderObject that may query the intrinsic size of its children. Examples of widgets that do this areIntrinsicHeight andIntrinsicWidth.

The default implementation ofRenderBox.performResize also uses the size computed bycomputeDryLayout to perform the resize. OverridingperformResize is therefore no longer necessary.

Migration guide

#

Subclasses that already overrideperformResize can be migrated by simply changing the function signature fromvoid performResize() toSize computeDryLayout(BoxConstraints constraints) and by returning the calculated size instead of assigning it to thesize setter. The old implementation ofperformResize can be removed.

Code before migration:

dart
@overridevoidperformResize(){size=constraints.biggest;}

Code after migration:

dart
// This replaces the old performResize method.@overrideSizecomputeDryLayout(BoxConstraintsconstraints){returnconstraints.biggest;}

If the subclass doesn't overrideperformResize, the implementation ofcomputeDryLayout has to be extracted from theperformLayout method. Basically,computeDryLayout needs to do all the workperformLayout is doing to figure out the size of theRenderBox. However, instead of assigning it to thesize setter, it returns the computed size. IfcomputeDryLayout needs to know the size of its children, it must obtain that size by callinggetDryLayout on the child instead of callinglayout.

If for some reason it is impossible to calculate the dry layout,computeDryLayout must calldebugCannotComputeDryLayout from within an assert and return a dummy size ofconst Size(0, 0). Calculating a dry layout is, for example, impossible if the size of aRenderBox depends on the baseline metrics of its children.

dart
@overrideSizecomputeDryLayout(BoxConstraintsconstraints){assert(debugCannotComputeDryLayout(reason:'Layout requires baseline metrics, which are only available after a full layout.'));returnconstSize(0,0);}

Timeline

#

Landed in version: 1.25.0-4.0.pre
In stable release: 2.0.0

References

#

API documentation:

Relevant issues:

Relevant PRs:

Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Flutter 3.38.6. Page last updated on 2025-10-28.View source orreport an issue.


[8]ページ先頭

©2009-2026 Movatter.jp