我有一个文本小部件,如果超过一定大小,可以将其截断:
ConstrainedBox(
constraints: BoxConstraints(maxHeight: 50.0),
child: Text(
widget.review,
overflow: TextOverflow.ellipsis,
)
);
或最大行数:
RichText(
maxLines: 2,
overflow: TextOverflow.ellipsis,
text: TextSpan(
style: TextStyle(color: Colors.black),
text: widget.review,
));
我的目标是仅在完成溢出后使文本可扩展。有没有适当的方法来检查文本是否溢出?
我发现RichText中有一个RenderParagraph renderObject
,它具有一个私有属性TextPainter _textPainter
,其中一个bool didExceedMaxLines
。
简而言之,我只需要访问richText.renderObject._textPainter.didExceedMaxLines
,但是正如您所看到的,它在下划线处是私有的。
答案 0 :(得分:5)
如果文本溢出与否,有一种更短的方法可以得到答案。你只需要定义 textStyle 并从这个方法中得到答案
bool hasTextOverflow(
String text,
TextStyle style,
{double minWidth = 0,
double maxWidth = double.infinity,
int maxLines = 2
}) {
final TextPainter textPainter = TextPainter(
text: TextSpan(text: text, style: style),
maxLines: maxLines,
textDirection: TextDirection.ltr,
)..layout(minWidth: minWidth, maxWidth: maxWidth);
return textPainter.didExceedMaxLines;
}
答案 1 :(得分:4)
您可以在pub.dev使用flutter插件auto_size_text
。
当文本溢出时,您可以设置一些小部件显示。
int maxLines = 3;
String caption = 'Your caption is here';
return AutoSizeText(
caption,
maxLines: maxLines,
style: TextStyle(fontSize: 20),
minFontSize: 15,
overflowReplacement: Column( // This widget will be replaced.
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
caption,
maxLines: maxLines,
overflow: TextOverflow.ellipsis,
),
Text(
"Show more",
style: TextStyle(color: PrimaryColor.kGrey),
)
],
),
);
答案 2 :(得分:3)
我找到了一种方法。完整的代码如下,但总之:
这是完整的演示应用程序:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Text Overflow Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(title: Text("DEMO")),
body: TextOverflowDemo(),
),
);
}
}
class TextOverflowDemo extends StatefulWidget {
@override
_EditorState createState() => _EditorState();
}
class _EditorState extends State<TextOverflowDemo> {
var controller = TextEditingController();
@override
void initState() {
controller.addListener(() {
setState(() {
mytext = controller.text;
});
});
controller.text = "This is a long overflowing text!!!";
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
String mytext = "";
@override
Widget build(BuildContext context) {
int maxLines = 1;
double fontSize = 30.0;
return Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
children: <Widget>[
LayoutBuilder(builder: (context, size) {
// Build the textspan
var span = TextSpan(
text: mytext,
style: TextStyle(fontSize: fontSize),
);
// Use a textpainter to determine if it will exceed max lines
var tp = TextPainter(
maxLines: maxLines,
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
text: span,
);
// trigger it to layout
tp.layout(maxWidth: size.maxWidth);
// whether the text overflowed or not
var exceeded = tp.didExceedMaxLines;
return Column(children: <Widget>[
Text.rich(
span,
overflow: TextOverflow.ellipsis,
maxLines: maxLines,
),
Text(exceeded ? "Overflowed!" : "Not overflowed yet.")
]);
}),
TextField(
controller: controller,
),
],
),
);
}
}