Files
MWClash/lib/common/measure.dart

94 lines
2.4 KiB
Dart
Raw Normal View History

2024-04-30 23:38:49 +08:00
import 'package:fl_clash/common/common.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class Measure {
final TextScaler _textScaler;
final BuildContext context;
final Map<String, dynamic> _measureMap;
2024-04-30 23:38:49 +08:00
Measure.of(this.context, double textScaleFactor)
: _measureMap = {},
_textScaler = TextScaler.linear(textScaleFactor);
2024-04-30 23:38:49 +08:00
Size computeTextSize(Text text, {double maxWidth = double.infinity}) {
2024-04-30 23:38:49 +08:00
final textPainter = TextPainter(
text: TextSpan(text: text.data, style: text.style),
2024-04-30 23:38:49 +08:00
maxLines: text.maxLines,
textScaler: _textScaler,
2024-04-30 23:38:49 +08:00
textDirection: text.textDirection ?? TextDirection.ltr,
)..layout(maxWidth: maxWidth);
2024-04-30 23:38:49 +08:00
return textPainter.size;
}
2024-06-06 16:31:08 +08:00
double get bodyMediumHeight {
return _measureMap.updateCacheValue(
'bodyMediumHeight',
() => computeTextSize(
Text('X', style: context.textTheme.bodyMedium),
).height,
);
2024-04-30 23:38:49 +08:00
}
double get bodyLargeHeight {
return _measureMap.updateCacheValue(
'bodyLargeHeight',
() =>
computeTextSize(Text('X', style: context.textTheme.bodyLarge)).height,
);
}
2024-06-06 16:31:08 +08:00
double get bodySmallHeight {
return _measureMap.updateCacheValue(
'bodySmallHeight',
() =>
computeTextSize(Text('X', style: context.textTheme.bodySmall)).height,
);
2024-04-30 23:38:49 +08:00
}
2024-06-06 16:31:08 +08:00
double get labelSmallHeight {
return _measureMap.updateCacheValue(
'labelSmallHeight',
() => computeTextSize(
Text('X', style: context.textTheme.labelSmall),
).height,
);
}
double get titleSmallHeight {
return _measureMap.updateCacheValue(
'titleSmallHeight',
() => computeTextSize(
Text('X', style: context.textTheme.titleSmall),
).height,
);
2024-04-30 23:38:49 +08:00
}
double get labelMediumHeight {
return _measureMap.updateCacheValue(
'labelMediumHeight',
() => computeTextSize(
Text('X', style: context.textTheme.labelMedium),
).height,
);
}
2024-06-06 16:31:08 +08:00
double get titleLargeHeight {
return _measureMap.updateCacheValue(
'titleLargeHeight',
() => computeTextSize(
Text('X', style: context.textTheme.titleLarge),
).height,
);
2024-04-30 23:38:49 +08:00
}
2024-06-06 16:31:08 +08:00
double get titleMediumHeight {
return _measureMap.updateCacheValue(
'titleMediumHeight',
() => computeTextSize(
Text('X', style: context.textTheme.titleMedium),
).height,
);
2024-06-06 16:31:08 +08:00
}
2024-04-30 23:38:49 +08:00
}