@@ -59,24 +59,9 @@ class CssBoxWidget extends StatelessWidget {
59
59
final direction = _checkTextDirection (context, textDirection);
60
60
final padding = style.padding? .resolve (direction);
61
61
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
-
78
62
return _CSSBoxRenderer (
79
- width: maxWidthCalculated ?? style.width ?? Width .auto (),
63
+ width:
64
+ _calculateMaxedWidth (context, style) ?? style.width ?? Width .auto (),
80
65
height: style.height ?? Height .auto (),
81
66
paddingSize: padding? .collapsedSize ?? Size .zero,
82
67
borderSize: style.border? .dimensions.collapsedSize ?? Size .zero,
@@ -106,6 +91,29 @@ class CssBoxWidget extends StatelessWidget {
106
91
);
107
92
}
108
93
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
+
109
117
/// Takes a list of InlineSpan children and generates a Text.rich Widget
110
118
/// containing those children.
111
119
static Widget _generateWidgetChild (List <InlineSpan > children, Style style) {
0 commit comments