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 {
|
2025-04-18 17:50:46 +08:00
|
|
|
final TextScaler _textScaler;
|
2025-02-09 18:39:38 +08:00
|
|
|
final BuildContext context;
|
2025-04-18 17:50:46 +08:00
|
|
|
final Map<String, dynamic> _measureMap;
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2025-04-18 17:50:46 +08:00
|
|
|
Measure.of(this.context, double textScaleFactor)
|
|
|
|
|
: _measureMap = {},
|
|
|
|
|
_textScaler = TextScaler.linear(
|
2025-03-12 17:15:31 +08:00
|
|
|
textScaleFactor,
|
|
|
|
|
);
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2025-02-03 23:32:00 +08:00
|
|
|
Size computeTextSize(
|
|
|
|
|
Text text, {
|
|
|
|
|
double maxWidth = double.infinity,
|
|
|
|
|
}) {
|
2024-04-30 23:38:49 +08:00
|
|
|
final textPainter = TextPainter(
|
2025-02-09 18:39:38 +08:00
|
|
|
text: TextSpan(
|
|
|
|
|
text: text.data,
|
2025-03-12 17:15:31 +08:00
|
|
|
style: text.style,
|
2025-02-09 18:39:38 +08:00
|
|
|
),
|
2024-04-30 23:38:49 +08:00
|
|
|
maxLines: text.maxLines,
|
2025-04-18 17:50:46 +08:00
|
|
|
textScaler: _textScaler,
|
2024-04-30 23:38:49 +08:00
|
|
|
textDirection: text.textDirection ?? TextDirection.ltr,
|
2025-02-03 23:32:00 +08:00
|
|
|
)..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 {
|
2025-04-18 17:50:46 +08:00
|
|
|
return _measureMap.updateCacheValue(
|
|
|
|
|
"bodyMediumHeight",
|
|
|
|
|
() => computeTextSize(
|
|
|
|
|
Text(
|
|
|
|
|
"X",
|
|
|
|
|
style: context.textTheme.bodyMedium,
|
|
|
|
|
),
|
|
|
|
|
).height,
|
|
|
|
|
);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-18 17:50:46 +08:00
|
|
|
double get bodyLargeHeight {
|
|
|
|
|
return _measureMap.updateCacheValue(
|
|
|
|
|
"bodyLargeHeight",
|
|
|
|
|
() => computeTextSize(
|
|
|
|
|
Text(
|
|
|
|
|
"X",
|
|
|
|
|
style: context.textTheme.bodyLarge,
|
|
|
|
|
),
|
|
|
|
|
).height,
|
2024-09-26 14:29:04 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 16:31:08 +08:00
|
|
|
double get bodySmallHeight {
|
2025-04-18 17:50:46 +08:00
|
|
|
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 {
|
2025-04-18 17:50:46 +08:00
|
|
|
return _measureMap.updateCacheValue(
|
|
|
|
|
"labelSmallHeight",
|
|
|
|
|
() => computeTextSize(
|
|
|
|
|
Text(
|
|
|
|
|
"X",
|
|
|
|
|
style: context.textTheme.labelSmall,
|
|
|
|
|
),
|
|
|
|
|
).height,
|
|
|
|
|
);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-07 17:22:55 +08:00
|
|
|
double get labelMediumHeight {
|
2025-04-18 17:50:46 +08:00
|
|
|
return _measureMap.updateCacheValue(
|
|
|
|
|
"labelMediumHeight",
|
|
|
|
|
() => computeTextSize(
|
|
|
|
|
Text(
|
|
|
|
|
"X",
|
|
|
|
|
style: context.textTheme.labelMedium,
|
|
|
|
|
),
|
|
|
|
|
).height,
|
|
|
|
|
);
|
2024-06-07 17:22:55 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-06 16:31:08 +08:00
|
|
|
double get titleLargeHeight {
|
2025-04-18 17:50:46 +08:00
|
|
|
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 {
|
2025-04-18 17:50:46 +08:00
|
|
|
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
|
|
|
}
|