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

套件詳情v1.0.33

substring_highlight

在字元級別突出顯示Flutter文字,以實現簡單且可自訂的搜尋詞高亮。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
7.5萬
喜歡數
200
Pub 分數
150

安裝套件

flutter pub add substring_highlight

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter 套件,可實現字元級別的文字強調,非常適合在文字中突顯搜尋詞彙。它提供簡單的整合方式與可自訂的匹配子字串樣式選項,適用於搜尋介面。此套件支援多個平台,包括 Android、iOS、Web、macOS、Windows 與 Linux,確保跨裝置行為一致。透過 pub 命令進行最小設定,開發者可清楚標記搜尋過程中的相關文字區段,進而提升使用者體驗。

適用情境

  • 搜尋結果預覽
  • 帶有視覺提示的文本過濾
  • 自定義搜尋 UI 組件
  • 即時關鍵字高亮
  • 表單中的使用者輸入回饋
  • 文件或內容掃描

主要特色

  • 字元級子字串匹配
  • 可自定義強調顏色和樣式
  • 簡單的 API 用於整合
  • 跨平台相容性
  • 支援多個文字小工具
  • 可設定的匹配靈敏度

適合對象

  • 開發搜尋密集型應用的開發者
  • 需要視覺反饋的UI設計師
  • 需要即時文字強調的應用程式
  • 教育或文件審查工具
  • 內容發現介面

使用建議

  • 可能影響非常長文字的效能
  • 需要手動處理文字更新
  • 僅限子字串比對(不支援正則表達式)
  • 樣式可能需要平台特定的調整
  • 不支援嵌套文字範圍

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

substring_highlight

Highlight Flutter text at the character-level.

Designed for case-insensitive search-term highlighting, a single search term sub-string is highlighted (perhaps multiple times) within a longer string.

Inspired by the existing Flutter package "highlight_text" which supports word matches ('Peter''), but substring_highlight has more granular character-by-character matches (e.g., 't' in 'Peter').

Limitations:

  1. Highlighted text is not clickable
  2. Doesn't solve perplexing world problems like hunger and climate change

The substrings being searched for highlighting don't have to match at the beginning of the longer strings (can be anywhere inside).

Even space characters will match, but not be highlighted, obviously.

Ancestor MUST have textDirection set (required by internal RichText widget), either through MaterialApp widget or explicitly wrapped by a Directionality widget:

Directionality(
    child: SubstringHighlight(text: 'Peter', term: 't'),
    textDirection: TextDirection.ltr)

Pull Requests

Pull requests are welcome and I try to merge them within a few days.

Examples

Default Styling Example

Code:

As an example, the following code snippet uses this package to highlight matching characters in each search result:

import 'package:substring_highlight/substring_highlight.dart';

...

  @override
  Widget build(BuildContext context) (
    return Container(
      padding: const EdgeInsets.all(12),
      child: SubstringHighlight(
        text: dropDownItem,     // search result string from database or something
        term: searchTerm,       // user typed "et"
      ),
    );
  )
Output:

Screenshot

Customized Styling Example

Code:

This example adds 'textStyle' and 'textStyleHighlight' to change the colors of the text:

import 'package:substring_highlight/substring_highlight.dart';

...

  @override
  Widget build(BuildContext context) (
    return Container(
      padding: const EdgeInsets.all(12),
      child: SubstringHighlight(
        text: dropDownItem,                         // each string needing highlighting
        term: searchTerm,                           // user typed "m4a"        
        textStyle: TextStyle(                       // non-highlight style                       
          color: Colors.grey,
        ),
        textStyleHighlight: TextStyle(              // highlight style
          color: Colors.black,
          decoration: TextDecoration.underline,
        ),        
      ),
    );
  )
Output:

Screenshot

所屬分類

原始碼儲存庫

使用此套件的專案