首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在颤振中扩展RichText

在颤振中扩展RichText
EN

Stack Overflow用户
提问于 2021-02-19 01:36:03
回答 1查看 1.5K关注 0票数 0

Message Widget以RichText Widget作为子容器返回一个容器。如果RichText的内容超过了某个预定义的限制,例如100个字符,那么当点击"Read“时,我想添加尾随文本"Read”。我偶然发现了一个中篇,它谈到了如何在TextSpan中使用CustomPainter。对于RichText,我需要类似的东西。

代码语言:javascript
复制
class Message extends StatelessWidget {
  final Widget _content;


  Message()
      : this._content = Content(),
        super(key: key);


  @override
  Widget build(BuildContext context) {
        ...
        return new Container(
            child: _content,
        );

  }



class Content extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    List<TextSpan> parsedText = createTextSpan(...);
    return RichText(
      text: TextSpan(children: parsedText),
    );
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-19 06:10:41

工作实例:

代码语言:javascript
复制
class ExpandableText extends StatefulWidget {
  final String text;
  final int trimLines;
  final TextStyle style;

  const ExpandableText(
    this.text, {
    Key key,
    this.trimLines = 2,
    this.style,
  })  : assert(text != null),
        super(key: key);

  @override
  ExpandableTextState createState() => ExpandableTextState();
}

class ExpandableTextState extends State<ExpandableText> {
  bool _readMore = true;
  void _onTapLink() {
    setState(() => _readMore = !_readMore);
  }

  @override
  Widget build(BuildContext context) {
    TextSpan link = TextSpan(
        text: _readMore
            ? '... Read more'
            : ' Read less',
        style: TextStyle(
          color: Colors.blue,
        ),
        recognizer: TapGestureRecognizer()..onTap = _onTapLink);
    Widget result = LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        assert(constraints.hasBoundedWidth);
        final double maxWidth = constraints.maxWidth;
        // Create a TextSpan with data
        final text = TextSpan(
          text: widget.text,
          style: widget.style,
        );
        // Layout and measure link
        TextPainter textPainter = TextPainter(
          text: link,
          textDirection: TextDirection
              .rtl, //better to pass this from master widget if ltr and rtl both supported
          maxLines: widget.trimLines,
          ellipsis: '...',
        );
        textPainter.layout(minWidth: constraints.minWidth, maxWidth: maxWidth);
        final linkSize = textPainter.size;
        // Layout and measure text
        textPainter.text = text;
        textPainter.layout(minWidth: constraints.minWidth, maxWidth: maxWidth);
        final textSize = textPainter.size;
        // Get the endIndex of data
        int endIndex;
        final pos = textPainter.getPositionForOffset(Offset(
          textSize.width - linkSize.width,
          textSize.height,
        ));
        endIndex = textPainter.getOffsetBefore(pos.offset);
        var textSpan;
        if (textPainter.didExceedMaxLines) {
          textSpan = TextSpan(
            text: _readMore ? widget.text.substring(0, endIndex) : widget.text,
            style: widget.style,
            children: <TextSpan>[link],
          );
        } else {
          textSpan = TextSpan(
            text: widget.text,
            style: widget.style,
          );
        }
        return RichText(
          softWrap: true,
          overflow: TextOverflow.clip,
          text: textSpan,
        );
      },
    );
    return result;
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66270607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档