improved performance and battery usage on older devices
This commit is contained in:
@@ -1,16 +1,29 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
extension TextExt on Text {
|
||||
Size get size {
|
||||
final textPainter = TextPainter(
|
||||
/// Single-line width as rendered in [context] (honours the text scaler).
|
||||
/// Memoised: chat bubbles measure the same few names and times over and
|
||||
/// over, and every measurement is a full paragraph layout.
|
||||
double measuredWidth(BuildContext context) {
|
||||
final scaler = MediaQuery.textScalerOf(context);
|
||||
final key = (data, style, scaler);
|
||||
final cached = _widthCache[key];
|
||||
if (cached != null) return cached;
|
||||
final painter = TextPainter(
|
||||
text: TextSpan(text: data, style: style),
|
||||
maxLines: 1,
|
||||
textDirection: TextDirection.ltr,
|
||||
)..layout(minWidth: 0, maxWidth: double.infinity);
|
||||
return textPainter.size;
|
||||
textScaler: scaler,
|
||||
)..layout();
|
||||
final width = painter.width;
|
||||
painter.dispose();
|
||||
if (_widthCache.length >= 512) _widthCache.clear();
|
||||
return _widthCache[key] = width;
|
||||
}
|
||||
}
|
||||
|
||||
final Map<(String?, TextStyle?, TextScaler), double> _widthCache = {};
|
||||
|
||||
/// Returns the first non-empty (after trim) entry, or '' if none match.
|
||||
String firstNonEmpty(List<String?> values) {
|
||||
for (final v in values) {
|
||||
|
||||
Reference in New Issue
Block a user