Skip to content

Commit 4d1667c

Browse files
TheCarpetMerchantSub6Resources
authored andcommitted
Added the 'max-width' css attribute
1 parent 2814407 commit 4d1667c

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
@@ -654,6 +654,10 @@ Style declarationsToStyle(Map<String, List<css.Expression>> declarations) {
654654
style.width =
655655
ExpressionMapping.expressionToWidth(value.first) ?? style.width;
656656
break;
657+
case 'max-width':
658+
style.maxWidth = ExpressionMapping.expressionToWidth(value.first) ??
659+
style.maxWidth;
660+
break;
657661
}
658662
}
659663
});
@@ -1228,6 +1232,8 @@ class ExpressionMapping {
12281232
double.parse(value.text.replaceAll(RegExp(r'\s+(\d+\.\d+)\s+'), ''));
12291233
Unit unit = _unitMap(value.unit);
12301234
return LengthOrPercent(number, unit);
1235+
} else if (value is css.PercentageTerm) {
1236+
return LengthOrPercent(double.parse(value.text), Unit.percent);
12311237
}
12321238

12331239
//Ignore un-parsable input

lib/src/style.dart

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

195+
/// CSS attribute "`max-width`"
196+
///
197+
/// Inherited: no,
198+
/// Default: null
199+
Width? maxWidth;
200+
195201
/// CSS attribute "`word-spacing`"
196202
///
197203
/// Inherited: yes,
@@ -261,6 +267,7 @@ class Style {
261267
this.verticalAlign = VerticalAlign.baseline,
262268
this.whiteSpace,
263269
this.width,
270+
this.maxWidth,
264271
this.wordSpacing,
265272
this.before,
266273
this.after,
@@ -353,6 +360,7 @@ class Style {
353360
verticalAlign: other.verticalAlign,
354361
whiteSpace: other.whiteSpace,
355362
width: other.width,
363+
maxWidth: other.maxWidth,
356364
wordSpacing: other.wordSpacing,
357365
before: other.before,
358366
after: other.after,
@@ -438,6 +446,7 @@ class Style {
438446
VerticalAlign? verticalAlign,
439447
WhiteSpace? whiteSpace,
440448
Width? width,
449+
Width? maxWidth,
441450
double? wordSpacing,
442451
String? before,
443452
String? after,
@@ -481,6 +490,7 @@ class Style {
481490
verticalAlign: verticalAlign ?? this.verticalAlign,
482491
whiteSpace: whiteSpace ?? this.whiteSpace,
483492
width: width ?? this.width,
493+
maxWidth: maxWidth ?? this.maxWidth,
484494
wordSpacing: wordSpacing ?? this.wordSpacing,
485495
before: beforeAfterNull == true ? null : before ?? this.before,
486496
after: beforeAfterNull == true ? null : after ?? this.after,

0 commit comments

Comments
 (0)