Tob*_*aut 3 android dart flutter flutter-android
我尝试使用 aTextPainter
来获取 Text Widget 中字符串的最大长度,但是如果我调用 Painter,它会抛出!_needsLayout': is not true.
异常。
例外
The following assertion was thrown building FeedPage(dirty, dependencies: [MediaQuery], state: _FeedPageState#9c489):
'package:flutter/src/painting/text_painter.dart': Failed assertion: line 546 pos 12: '!_needsLayout': is not true.
Run Code Online (Sandbox Code Playgroud)
使用 TextPainter 的方法
int maxCharCountToFit(String content) {
List<String> splitted = content.split(" ");
for (int i = splitted.length; i >= 0; i--) {
bool retry = TextPainter(
text: TextSpan(text: splitted.sublist(0, splitted.length - i).join(" "), style: pageTextStyle),
maxLines: 25,
textScaleFactor: MediaQuery.of(context).textScaleFactor,
textDirection: TextDirection.ltr,
).didExceedMaxLines ==
false;
if (retry == false) {
return splitted.sublist(0, i).length;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
完整文件
归档时间: |
|
查看次数: |
1562 次 |
最近记录: |