parseDoubleWithUnits method
Parses a rawDouble String to a double
taking into account absolute and relative units
(px, em or ex).
Passing an em value will calculate the result
relative to the provided fontSize:
1 em = 1 * fontSize.
Passing an ex value will calculate the result
relative to the provided xHeight:
1 ex = 1 * xHeight.
The rawDouble might include a unit which is
stripped off when parsed to a double.
Passing null will return null.
Implementation
double? parseDoubleWithUnits(
String? rawDouble, {
bool tryParse = false,
}) {
if (rawDouble == null) {
return null;
}
double unit = 1.0;
// 1 rem unit is equal to the root font size.
// 1 em unit is equal to the current font size.
// 1 ex unit is equal to the current x-height.
if (rawDouble.contains('pt')) {
unit = kPointsToPixelFactor;
} else if (rawDouble.contains('rem')) {
_compatibilityTester.usesFontSize = true;
unit = theme.fontSize;
} else if (rawDouble.contains('em')) {
_compatibilityTester.usesFontSize = true;
unit = theme.fontSize;
} else if (rawDouble.contains('ex')) {
_compatibilityTester.usesFontSize = true;
unit = theme.xHeight;
}
final double? value = parseDouble(
rawDouble,
tryParse: tryParse,
);
return value != null ? value * unit : null;
}