From 430e75605a7ae4fe474afe32959de46e4da8d166 Mon Sep 17 00:00:00 2001 From: "justin02.zhang" Date: Tue, 7 Jan 2025 23:59:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:flex=E5=B8=83=E5=B1=80flexDirection?= =?UTF-8?q?=E6=98=AFrow=E5=9C=BA=E6=99=AF=E4=B8=8B=EF=BC=8C=E5=AD=90?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E5=8C=85=E5=90=AB=E5=92=8C=E4=B8=8D=E5=90=AB?= =?UTF-8?q?overflow:hidden=E4=B8=94=E5=AE=BD=E5=BA=A6=E8=B6=85=E8=BF=87?= =?UTF-8?q?=E7=88=B6=E6=8E=A7=E4=BB=B6=E6=97=B6ui=E6=A0=B7=E5=BC=8F?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E4=B8=8D=E5=AF=B9=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webf/lib/src/rendering/flex.dart | 52 ++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/webf/lib/src/rendering/flex.dart b/webf/lib/src/rendering/flex.dart index ba11b6c864..5aee0593c8 100644 --- a/webf/lib/src/rendering/flex.dart +++ b/webf/lib/src/rendering/flex.dart @@ -760,6 +760,9 @@ class RenderFlexLayout extends RenderLayoutBox { // Info about each flex item in each flex line Map runChildren = {}; + List overflowHiddenNodeId = []; + double overflowHiddenNodeTotalMinWidth = 0; + double overflowNotHiddenNodeTotalWidth = 0; for (RenderBox child in children) { final RenderLayoutParentData? childParentData = child.parentData as RenderLayoutParentData?; BoxConstraints childConstraints; @@ -847,8 +850,8 @@ class RenderFlexLayout extends RenderLayoutBox { // Baseline alignment in column direction behave the same as flex-start. AlignSelf alignSelf = _getAlignSelf(child); bool isBaselineAlign = alignSelf == AlignSelf.baseline || renderStyle.alignItems == AlignItems.baseline; - - if (_isHorizontalFlexDirection && isBaselineAlign) { + bool isHorizontal = _isHorizontalFlexDirection; + if (isHorizontal && isBaselineAlign) { // Distance from top to baseline of child double childAscent = _getChildAscent(child); double? childMarginTop = 0; @@ -870,9 +873,10 @@ class RenderFlexLayout extends RenderLayoutBox { runCrossAxisExtent = math.max(runCrossAxisExtent, childCrossAxisExtent); } + double _originalMainSize = _getMainSize(child, shouldUseIntrinsicMainSize: true); runChildren[childNodeId] = _RunChild( child, - _getMainSize(child, shouldUseIntrinsicMainSize: true), + _originalMainSize, 0, false, ); @@ -889,9 +893,51 @@ class RenderFlexLayout extends RenderLayoutBox { if (flexShrink > 0) { totalFlexShrink += flexShrink; } + if (isHorizontal && child is RenderBoxModel) { + if (child.renderStyle.overflowX == CSSOverflowType.hidden) { + overflowHiddenNodeId.add(childNodeId); + overflowHiddenNodeTotalMinWidth += child.contentConstraints?.minWidth ?? 0; + } else { + overflowNotHiddenNodeTotalWidth += _originalMainSize; + } + } } if (runChildren.isNotEmpty) { + if (overflowHiddenNodeId.isNotEmpty && runMainAxisExtent > flexLineLimit && overflowHiddenNodeTotalMinWidth < flexLineLimit) { + int _overflowNodeSize = overflowHiddenNodeId.length; + double overWidth = flexLineLimit - overflowNotHiddenNodeTotalWidth; + double avgOverWidth = overWidth / _overflowNodeSize; + int index = 0; + for (int nodeId in overflowHiddenNodeId) { + _RunChild? _runChild = runChildren[nodeId]; + if (_runChild != null) { + index += 1; + if (overWidth <= 0) {// 剩余宽度少于0情况 + BoxConstraints oldConstraints = _runChild.child.constraints; + double _minWidth = oldConstraints.minWidth; + _runChild.child.layout(BoxConstraints(minWidth: _minWidth, maxWidth: _minWidth, minHeight: oldConstraints.minHeight, maxHeight: oldConstraints.maxHeight), parentUsesSize: true); + _runChild.originalMainSize = _minWidth; + _childrenIntrinsicMainSizes[nodeId] = _minWidth; + } else if (_runChild.originalMainSize <= avgOverWidth) {// 子控件实际宽度小于剩余宽度的平均值情况 + // child不需要做layout计算,调整平均值 + overWidth -= _runChild.originalMainSize; + avgOverWidth = overWidth / (_overflowNodeSize - index); + } else { + BoxConstraints oldConstraints = _runChild.child.constraints; + double _minWidth = oldConstraints.minWidth; + double _maxWidth = avgOverWidth; + if (_minWidth > _maxWidth) { + _maxWidth = _minWidth; + } + _runChild.child.layout(BoxConstraints(minWidth: _minWidth, maxWidth: _maxWidth, minHeight: oldConstraints.minHeight, maxHeight: oldConstraints.maxHeight), parentUsesSize: true); + _runChild.originalMainSize = avgOverWidth; + _childrenIntrinsicMainSizes[nodeId] = avgOverWidth; + } + } + } + runMainAxisExtent = flexLineLimit; + } _runMetrics.add(_RunMetrics( runMainAxisExtent, runCrossAxisExtent, totalFlexGrow, totalFlexShrink, maxSizeAboveBaseline, runChildren, 0)); }