搜索插件、代码仓与 APP 分类

expandable_text 封面
插件详情v2.3.0

expandable_text

仅显示文本第一行的部件,并添加一个链接以展开完整文本。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
3.2万
喜欢数
291
Pub 分数
140

安装插件

flutter pub add expandable_text

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

expandable_text 包提供了一个 Flutter 小部件,该小部件仅显示文本的初始行,并带有可点击的链接以展开完整内容。它非常适合以简洁、用户友好的方式管理长文本片段。该小部件支持展开或收起时的平滑动画,并可在所有主要平台(包括 Android、iOS、Web、macOS、Windows 和 Linux)上正常工作。它通过防止过度占用垂直空间来提升可读性,同时保持无障碍访问。

适合场景

  • 在列表中显示长描述
  • 显示截断的评论或评论
  • 通过大文本块提升UI性能
  • 创建交互式内容部分
  • 减少聊天界面中的屏幕杂乱

主要特点

  • 平滑的展开/折叠动画
  • 可自定义的可见行数
  • 可配置的展开/折叠文本
  • 支持富文本和样式
  • 跨平台兼容性
  • 与现有控件的轻松集成

适合对象

  • 包含长文本内容的应用程序
  • 需要动态文本扩展的用户界面
  • 寻求轻量级文本管理的开发者
  • 需要响应式文本布局的项目
  • 专注于极简主义的UI/UX设计师

使用建议

  • 可能需要额外的样式以保持外观一致
  • 使用超大文本时的性能影响
  • 对复杂布局的自定义功能有限
  • 不适用于非交互式文本显示
  • 需要谨慎处理文本溢出

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Build pub package codecov license

This Flutter package includes the widget ExpandableText which you can use to initially only show a defined number of lines of a probably long text. The widget appends a configurable text link which lets the user expand the full text, or collapse it again.

Example with maxLines=1

Getting started

Add this to your package's pubspec.yaml file:

dependencies:
  expandable_text: 2.3.0

Next, import the package into your dart code:

import 'package:expandable_text/expandable_text.dart';
Usage

Show an expandable text if longText exceeds one line:

Widget build(BuildContext context) {
    return ExpandableText(
        longText,
        expandText: 'show more',
        collapseText: 'show less',
        maxLines: 1,
        linkColor: Colors.blue,
    );
}
Advanced example

This example shows a message that was posted by a user. The username is always visible right before the text and tapping on it opens the user profile. The text is truncated after two lines and can be expanded by tapping on the link show more at the end or the text itself. After the text was expanded it cannot be collapsed again as no collapseText was provided. URLs, @mentions and #hashtags in the text are styled differently and can be tapped to open the browser or the user profile.

Widget build(BuildContext context) {
    return ExpandableText(
        message,
        expandText: 'show more',
        maxLines: 2,
        linkColor: Colors.blue,
        animation: true,
        collapseOnTextTap: true,
        prefixText: username,
        onPrefixTap: () => showProfile(username),
        prefixStyle: TextStyle(fontWeight: FontWeight.bold),
        onHashtagTap: (name) => showHashtag(name),
        hashtagStyle: TextStyle(
            color: Color(0xFF30B6F9),
        ),
        onMentionTap: (username) => showProfile(username),
        mentionStyle: TextStyle(
            fontWeight: FontWeight.w600,
        ),
        onUrlTap: (url) => launchUrl(url),
        urlStyle: TextStyle(
            decoration: TextDecoration.underline,
        ),
    );
}

Features

  • Link to expand the collapsed text (expandText)
  • Expand and collapse animation (animation, animationDuration, animationCurve)
  • Optional link to collapse the expanded text (collapseText)
  • Configure the style of the link (linkStyle / linkColor)
  • Control whether the ellipsis is part of the link (linkEllipsis)
  • Optional prefix text with style and tap callback (prefixText, prefixStyle, onPrefixTap)
  • Text with tappable and styled links (onUrlTap, urlStyle)
  • Text with tappable and styled @mentions (onMentionTap, mentionStyle)
  • Text with tappable and styled #hashtags (onHashtagTap, hashtagStyle)
  • Configure the number of visible lines of the collapsed text (maxLines)
  • Control the default expanded state (expanded, onLinkTap)
  • Callback for expanded changed event (onExpandedChanged)
  • Tap on the text to expand or collapse the text (expandOnTextTap, collapseOnTextTap)

Bugs and feature requests

Have a bug or a feature request? Please first search for existing and closed issues. If your problem or idea is not addressed yet, please open a new issue.

Copyright & License

Code copyright 2020–2022 Florian Weinaug. Code released under the MIT license.

所属分类

源码仓库

使用此插件的项目