搜尋套件、儲存庫與 APP 分類

expandable_text 封面
套件詳情v2.3.0

expandable_text

僅顯示文字第一行的組件,並新增一個連結以展開完整文字。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
2.7萬
喜歡數
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.

所屬分類

原始碼儲存庫

使用此套件的專案