Files
MWClash/lib/common/measure.dart

114 lines
2.5 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 _textScale;
final BuildContext context;
final String? _fontFamily;
2024-04-30 23:38:49 +08:00
Measure.of(this.context, {String? fontFamily})
: _textScale = TextScaler.linear(
WidgetsBinding.instance.platformDispatcher.textScaleFactor,
),
_fontFamily = fontFamily ?? "";
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?.copyWith(
fontFamily: _fontFamily,
),
),
2024-04-30 23:38:49 +08:00
maxLines: text.maxLines,
textScaler: _textScale,
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;
}
double? _bodyMediumHeight;
Size? _bodyLargeSize;
2024-04-30 23:38:49 +08:00
double? _bodySmallHeight;
double? _labelSmallHeight;
double? _labelMediumHeight;
2024-04-30 23:38:49 +08:00
double? _titleLargeHeight;
2024-06-06 16:31:08 +08:00
double? _titleMediumHeight;
2024-04-30 23:38:49 +08:00
2024-06-06 16:31:08 +08:00
double get bodyMediumHeight {
2024-04-30 23:38:49 +08:00
_bodyMediumHeight ??= computeTextSize(
Text(
"X",
2024-04-30 23:38:49 +08:00
style: context.textTheme.bodyMedium,
),
).height;
return _bodyMediumHeight!;
}
Size get bodyLargeSize {
_bodyLargeSize ??= computeTextSize(
Text(
"X",
style: context.textTheme.bodyLarge,
),
);
return _bodyLargeSize!;
}
2024-06-06 16:31:08 +08:00
double get bodySmallHeight {
2024-04-30 23:38:49 +08:00
_bodySmallHeight ??= computeTextSize(
Text(
"X",
2024-04-30 23:38:49 +08:00
style: context.textTheme.bodySmall,
),
).height;
return _bodySmallHeight!;
}
2024-06-06 16:31:08 +08:00
double get labelSmallHeight {
2024-04-30 23:38:49 +08:00
_labelSmallHeight ??= computeTextSize(
Text(
"X",
2024-04-30 23:38:49 +08:00
style: context.textTheme.labelSmall,
),
).height;
return _labelSmallHeight!;
}
double get labelMediumHeight {
_labelMediumHeight ??= computeTextSize(
Text(
"X",
style: context.textTheme.labelMedium,
),
).height;
return _labelMediumHeight!;
}
2024-06-06 16:31:08 +08:00
double get titleLargeHeight {
2024-04-30 23:38:49 +08:00
_titleLargeHeight ??= computeTextSize(
Text(
"X",
2024-04-30 23:38:49 +08:00
style: context.textTheme.titleLarge,
),
).height;
return _titleLargeHeight!;
}
2024-06-06 16:31:08 +08:00
double get titleMediumHeight {
_titleMediumHeight ??= computeTextSize(
Text(
"X",
2024-06-06 16:31:08 +08:00
style: context.textTheme.titleMedium,
),
).height;
return _titleMediumHeight!;
}
2024-04-30 23:38:49 +08:00
}