Skip to content

Commit fecfece

Browse files
Added the 'max-width' css attribute
1 parent 79ec194 commit fecfece

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

lib/src/css_box_widget.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,24 @@ 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+
6278
return _CSSBoxRenderer(
63-
width: style.width ?? Width.auto(),
79+
width: maxWidthCalculated ?? style.width ?? Width.auto(),
6480
height: style.height ?? Height.auto(),
6581
paddingSize: padding?.collapsedSize ?? Size.zero,
6682
borderSize: style.border?.dimensions.collapsedSize ?? Size.zero,

lib/src/css_parser.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,10 @@ Style declarationsToStyle(Map<String, List<css.Expression>> declarations) {
656656
style.width =
657657
ExpressionMapping.expressionToWidth(value.first) ?? style.width;
658658
break;
659+
case 'max-width':
660+
style.maxWidth = ExpressionMapping.expressionToWidth(value.first) ??
661+
style.maxWidth;
662+
break;
659663
}
660664
}
661665
});
@@ -1230,6 +1234,8 @@ class ExpressionMapping {
12301234
double.parse(value.text.replaceAll(RegExp(r'\s+(\d+\.\d+)\s+'), ''));
12311235
Unit unit = _unitMap(value.unit);
12321236
return LengthOrPercent(number, unit);
1237+
} else if (value is css.PercentageTerm) {
1238+
return LengthOrPercent(double.parse(value.text), Unit.percent);
12331239
}
12341240

12351241
//Ignore un-parsable input

lib/src/style.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ class Style {
193193
/// Default: Width.auto()
194194
Width? width;
195195

196+
/// CSS attribute "`max-width`"
197+
///
198+
/// Inherited: no,
199+
/// Default: null
200+
Width? maxWidth;
201+
196202
/// CSS attribute "`word-spacing`"
197203
///
198204
/// Inherited: yes,
@@ -262,6 +268,7 @@ class Style {
262268
this.verticalAlign = VerticalAlign.baseline,
263269
this.whiteSpace,
264270
this.width,
271+
this.maxWidth,
265272
this.wordSpacing,
266273
this.before,
267274
this.after,
@@ -355,6 +362,7 @@ class Style {
355362
verticalAlign: other.verticalAlign,
356363
whiteSpace: other.whiteSpace,
357364
width: other.width,
365+
maxWidth: other.maxWidth,
358366
wordSpacing: other.wordSpacing,
359367
before: other.before,
360368
after: other.after,
@@ -440,6 +448,7 @@ class Style {
440448
VerticalAlign? verticalAlign,
441449
WhiteSpace? whiteSpace,
442450
Width? width,
451+
Width? maxWidth,
443452
double? wordSpacing,
444453
String? before,
445454
String? after,
@@ -483,6 +492,7 @@ class Style {
483492
verticalAlign: verticalAlign ?? this.verticalAlign,
484493
whiteSpace: whiteSpace ?? this.whiteSpace,
485494
width: width ?? this.width,
495+
maxWidth: maxWidth ?? this.maxWidth,
486496
wordSpacing: wordSpacing ?? this.wordSpacing,
487497
before: beforeAfterNull == true ? null : before ?? this.before,
488498
after: beforeAfterNull == true ? null : after ?? this.after,

0 commit comments

Comments
 (0)