flutter 字符串多余n行折叠,点击展开

  • 获取TextPainter
1
2
3
4
5
6
7
8
TextPainter fetchTextPainter(String text, TextStyle style, int maxLine,
double minWidth, double maxWidth) {
return TextPainter(
maxLines: maxLine,
text: TextSpan(text: text, style: style),
textDirection: TextDirection.ltr)
..layout(maxWidth: maxWidth, minWidth: minWidth);
}
  • 判断是否超过n行,需要截断文本
1
2
3
4
5
6
7
8
9
10
11
bool isExpansion(String text, TextStyle style, int maxLine, double minWidth,
double maxWidth) {
TextPainter _textPainter =
fetchTextPainter(text, style, maxLine, minWidth, maxWidth);
if (_textPainter.didExceedMaxLines) {
//这里判断 文本是否截断
return true;
} else {
return false;
}
}
  • 超过n行,需要展开。展开箭头放在文本右下角,移除两个字符(根据需求)用来放展开箭头
1
2
3
4
5
6
7
8
9
10
String expansionString(String text, TextStyle style, int maxLine,
double minWidth, double maxWidth) {
TextPainter _textPainter =
fetchTextPainter(text, style, maxLine, minWidth, maxWidth);
var end = _textPainter
.getPositionForOffset(
Offset(_textPainter.size.width, _textPainter.size.height))
.offset;
return text.substring(0, end - 2) + "...";
}