Skip to content

Commit 36db0e8

Browse files
maxWidth calculation in _calculateMaxedWidth, annotated
1 parent dc6f72a commit 36db0e8

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

lib/src/css_box_widget.dart

+25-17
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,9 @@ class CssBoxWidget extends StatelessWidget {
5959
final direction = _checkTextDirection(context, textDirection);
6060
final padding = style.padding?.resolve(direction);
6161

62-
Width? maxWidthCalculated;
63-
if (style.maxWidth != null && style.width != null) {
64-
if (style.maxWidth!.unit == Unit.percent &&
65-
style.width!.unit == Unit.px) {
66-
// If our max is a percentage, we want to look at the size available and not be bigger than that.
67-
try {
68-
double width =
69-
MediaQuery.of(context).size.width * (style.maxWidth!.value / 100);
70-
maxWidthCalculated = Width(width, style.width!.unit);
71-
} catch (_) {}
72-
} else if (style.width!.unit == style.maxWidth!.unit &&
73-
style.width!.value > style.maxWidth!.value) {
74-
maxWidthCalculated = Width(style.maxWidth!.value, style.maxWidth!.unit);
75-
}
76-
}
77-
7862
return _CSSBoxRenderer(
79-
width: maxWidthCalculated ?? style.width ?? Width.auto(),
63+
width:
64+
_calculateMaxedWidth(context, style) ?? style.width ?? Width.auto(),
8065
height: style.height ?? Height.auto(),
8166
paddingSize: padding?.collapsedSize ?? Size.zero,
8267
borderSize: style.border?.dimensions.collapsedSize ?? Size.zero,
@@ -106,6 +91,29 @@ class CssBoxWidget extends StatelessWidget {
10691
);
10792
}
10893

94+
/// Returns the width capped with maxWidth if necessary.
95+
static Width? _calculateMaxedWidth(BuildContext context, Style style) {
96+
// We only need to calculate something if we have a width and it needs to be capped.
97+
if (style.maxWidth == null || style.width == null) return null;
98+
99+
// If our max is a percentage, we want to look at the size available and not be bigger than that.
100+
// TODO In the else case, we should have something to compare across different units, as for now some cases won't be handled.
101+
if (style.maxWidth!.unit == Unit.percent && style.width!.unit == Unit.px) {
102+
return Width(
103+
MediaQuery.of(context).size.width * (style.maxWidth!.value / 100),
104+
style.width!.unit,
105+
);
106+
} else if (style.width!.unit == style.maxWidth!.unit &&
107+
style.width!.value > style.maxWidth!.value) {
108+
return Width(
109+
style.maxWidth!.value,
110+
style.maxWidth!.unit,
111+
);
112+
} else {
113+
return null;
114+
}
115+
}
116+
109117
/// Takes a list of InlineSpan children and generates a Text.rich Widget
110118
/// containing those children.
111119
static Widget _generateWidgetChild(List<InlineSpan> children, Style style) {

0 commit comments

Comments
 (0)