Skip to content

Commit 9dc7f08

Browse files
committed
fix: Use enum instead of const int internally in length.dart
1 parent a62449a commit 9dc7f08

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

lib/src/style/length.dart

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
1-
/// Increase new base unit types' values by a factor of 2 each time.
2-
const int _percent = 0x1;
3-
const int _length = 0x2;
4-
const int _auto = 0x4;
1+
/// These are the base unit types
2+
enum _UnitType {
3+
percent,
4+
length,
5+
auto,
6+
lengthPercent(children: [_UnitType.length, _UnitType.percent]),
7+
lengthPercentAuto(children: [_UnitType.length, _UnitType.percent, _UnitType.auto]);
58

6-
/// These values are combinations of the base unit-types
7-
const int _lengthPercent = _length | _percent;
8-
const int _lengthPercentAuto = _lengthPercent | _auto;
9+
final List<_UnitType> children;
10+
11+
const _UnitType({this.children = const []});
12+
13+
bool matches(_UnitType other) {
14+
return this == other || children.contains(other);
15+
}
16+
}
917

1018
/// A Unit represents a CSS unit
1119
enum Unit {
1220
//ch,
13-
em(_length),
21+
em(_UnitType.length),
1422
//ex,
15-
percent(_percent),
16-
px(_length),
17-
rem(_length),
23+
percent(_UnitType.percent),
24+
px(_UnitType.length),
25+
rem(_UnitType.length),
1826
//Q,
1927
//vh,
2028
//vw,
21-
auto(_auto);
29+
auto(_UnitType.auto);
2230

2331
const Unit(this.unitType);
24-
final int unitType;
32+
final _UnitType unitType;
2533
}
2634

2735
/// Represents a CSS dimension https://drafts.csswg.org/css-values/#dimensions
2836
abstract class Dimension {
2937
double value;
3038
Unit unit;
3139

32-
Dimension(this.value, this.unit, int _dimensionUnitType)
33-
: assert(
34-
identical((unit.unitType | _dimensionUnitType), _dimensionUnitType),
35-
"This dimension was given a Unit that isn't specified.");
40+
Dimension(this.value, this.unit, _UnitType _dimensionUnitType)
41+
: assert(_dimensionUnitType.matches(unit.unitType),
42+
"This Dimension was given a Unit that isn't specified.");
3643
}
3744

3845
/// This dimension takes a value with a length unit such as px or em. Note that
3946
/// these can be fixed or relative (but they must not be a percent)
4047
class Length extends Dimension {
41-
Length(double value, [Unit unit = Unit.px]) : super(value, unit, _length);
48+
Length(double value, [Unit unit = Unit.px]) : super(value, unit, _UnitType.length);
4249
}
4350

4451
/// This dimension takes a value with a length-percent unit such as px or em
4552
/// or %. Note that these can be fixed or relative (but they must not be a
4653
/// percent)
4754
class LengthOrPercent extends Dimension {
4855
LengthOrPercent(double value, [Unit unit = Unit.px])
49-
: super(value, unit, _lengthPercent);
56+
: super(value, unit, _UnitType.lengthPercent);
5057
}
5158

5259
class AutoOrLengthOrPercent extends Dimension {
5360
AutoOrLengthOrPercent(double value, [Unit unit = Unit.px])
54-
: super(value, unit, _lengthPercentAuto);
61+
: super(value, unit, _UnitType.lengthPercentAuto);
5562
}

0 commit comments

Comments
 (0)