我遇到的问题在使用html字符串时,是引入了html插件,但是在做一个展开、收起时,收起状态对
标签不解析问题。
收起状态下,因为要用到Text的maxLines: 3,,所以不能使用Html(),我这里遇到的只有
标签没有被解析,而且只展示三行数据,展示信息较少,万一我直接针对字符串进行处理,替换掉
,`String shortText = _text.replaceAll(’
’, ‘’);
具体代码如下:
KnowMore(summary:collegeMsg['summary']),
class KnowMore extends StatefulWidget {
String summary;
KnowMore({this.summary});
@override
_KnowMoreState createState() => _KnowMoreState();
}
class _KnowMoreState extends State<KnowMore> {
// 当前是否已是 "全文" 状态
bool mIsExpansion = false;
//最大显示行数(默认 3 行)
int mMaxLine = 3;
//_text:显示的文字
Widget _RichText(String _text) {
String shortText = _text.replaceAll('<p>', '');
if (IsExpansion(_text)) {
// //如果需要截断
if (mIsExpansion) {
return Column(
children: <Widget>[
Html(
data: '${_text}',
),
// new Text(
// _text,
// textAlign: TextAlign.left,
// ),
Align(
alignment: Alignment.centerRight,
child: FlatButton(
onPressed: () {
_isShowText();
},
child: Text(
"收起",
style:TextStyle(
color:Colors.blue,
),
),
//textColor: SQColor.grey,
),
),
],
);
} else {
return Column(
children: <Widget>[
// Html(
// data: _text,
// ),
new Text(
'${shortText}',
maxLines: 3,
textAlign: TextAlign.left,
overflow: TextOverflow.ellipsis,
),
Align(
alignment: Alignment.centerRight,
child: FlatButton(
onPressed: () {
_isShowText();
},
child: Text(
"展开",
style:TextStyle(
color:Colors.blue,
),
),
//textColor: SQColor.grey,
),
),
],
);
}
} else {
return Text(
_text,
maxLines: 3,
textAlign: TextAlign.left,
overflow: TextOverflow.ellipsis,
);
}
}
bool IsExpansion(String text) {
TextPainter _textPainter = TextPainter(
maxLines: 3,
text: TextSpan(
text: text, style: TextStyle(fontSize: 16.0, color: Colors.black)),
textDirection: TextDirection.ltr)
..layout(maxWidth: MediaQuery.of(context).size.width, minWidth: 300);
if (_textPainter.didExceedMaxLines) {//这里判断 文本是否截断
return true;
} else {
return false;
}
}
void _isShowText() {
if (mIsExpansion) {
//关闭了
setState(() {
mIsExpansion = false;
});
} else {
setState(() {
mIsExpansion = true;
});
}
}
@override
Widget build(BuildContext context) {
return Container(
//height:100,
child:_RichText(widget.summary),
);
}
}