Files
MWClash/lib/common/measure.dart

90 lines
2.0 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;
late BuildContext context;
2024-04-30 23:38:49 +08:00
Measure.of(this.context)
: _textScale = TextScaler.linear(
WidgetsBinding.instance.platformDispatcher.textScaleFactor);
2024-04-30 23:38:49 +08:00
Size computeTextSize(Text text) {
final textPainter = TextPainter(
text: TextSpan(text: text.data, style: text.style),
maxLines: text.maxLines,
textScaler: _textScale,
2024-04-30 23:38:49 +08:00
textDirection: text.textDirection ?? TextDirection.ltr,
)..layout();
return textPainter.size;
}
double? _bodyMediumHeight;
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(
"",
style: context.textTheme.bodyMedium,
),
).height;
return _bodyMediumHeight!;
}
2024-06-06 16:31:08 +08:00
double get bodySmallHeight {
2024-04-30 23:38:49 +08:00
_bodySmallHeight ??= computeTextSize(
Text(
"",
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(
"",
style: context.textTheme.labelSmall,
),
).height;
return _labelSmallHeight!;
}
double get labelMediumHeight {
_labelMediumHeight ??= computeTextSize(
Text(
"",
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(
"",
style: context.textTheme.titleLarge,
),
).height;
return _titleLargeHeight!;
}
2024-06-06 16:31:08 +08:00
double get titleMediumHeight {
_titleMediumHeight ??= computeTextSize(
Text(
"",
style: context.textTheme.titleMedium,
),
).height;
return _titleMediumHeight!;
}
2024-04-30 23:38:49 +08:00
}