refactor size calculation

This commit is contained in:
Fabian Freund
2025-07-30 22:05:25 +02:00
parent 6bc93f7b7d
commit 28a8588051
2 changed files with 49 additions and 40 deletions
+30
View File
@@ -0,0 +1,30 @@
import 'package:flutter/material.dart';
int _getLineCountUsingBoxes(String text, TextStyle style, double maxWidth) {
final textSpan = TextSpan(text: text, style: style);
final textPainter = TextPainter(
text: textSpan,
textDirection: TextDirection.ltr,
);
textPainter.layout(maxWidth: maxWidth);
// Select all text
final selection = TextSelection(baseOffset: 0, extentOffset: text.length);
// Each box represents one line
final lines = textPainter.getBoxesForSelection(selection);
return lines.length;
}
int? getTextFieldLineCount(GlobalKey key, String text, TextStyle style) {
final box = key.currentContext?.findRenderObject();
if (box case final RenderBox box) {
final width = box.size.width;
return _getLineCountUsingBoxes(text, style, width);
}
return null;
}