PrettyDiffText 是一個 Flutter 套件,包裝 RichText 以視覺方式強調兩個文字字串之間的差異。它利用 Google 的 diff-match-patch 庫進行準確且高效的文字比對,非常適合以易讀格式顯示編輯、變更或修訂。該套件支援多個平台,並能順暢整合至需要即時或歷史文字比對的應用程式中。透過顏色與格式清楚標記新增、刪除和修改內容,進而提升使用者體驗。
適用情境
在協作應用中顯示文字修訂
可視化程式碼或文件變更
在筆記應用中顯示編輯歷史
突出顯示版本化內容的差異
即時聊天或訊息中的文字對比
追蹤設定或設定的更新
主要特色
使用 diff-match-patch 進行精確比較
支援豐富文字格式
顏色編碼的新增與刪除
跨平台相容性
與現有 RichText 小部件的輕鬆整合
可自訂的樣式選項
適合對象
開發編輯或協作工具的開發者
需要變更追蹤的應用程式
教育或文件平台
版本控制介面
顯示動態文字更新的 UI
使用建議
可能增加簡單情況下的組件複雜度
長文字帶來的效能影響
需要仔細處理巢狀的富文字
超出基本樣式之外的有限自訂
依賴外部的 diff-match-patch 庫
未針對高頻率更新進行優化
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
PrettyDiffText
PrettyDiffText is wrapper of RichText which demonstrates differences between two texts visually.
By default, it uses Google's diff-match-patch library which implements Myer's diff algorithm. It is generally considered to be the best general-purpose diff.
:fire: Pure Dart: It is written purely in Dart.
:star: Cross-Platform: Works on Android, iOS, macOS, Windows, Linux and the web.
:boom: Highly Customizable: Almost everything can be customized:
Text style of AddedText, DeletedText, EqualText.
Diff cleanup types: SEMANTIC, EFFICIENCY, NONE.
Diff algorithm tuning: DiffTimeout, EditCost.
Bring your own diffs: calculate the diffs using your own algorithm.
PrettyDiffText(
oldText: "Your old text",
newText: "Your new text",
)
Customization
Those properties are available for customization:
/// The [oldText] is the original text to compare against./// The [newText] is the modified text to compare with.////// The [diffCleanupType] determines how the diff results are cleaned up:/// - [DiffCleanupType.SEMANTIC]: Reduces the number of edits by eliminating semantically trivial equalities (default)/// - [DiffCleanupType.EFFICIENCY]: Reduces the number of edits by eliminating operationally trivial equalities/// - [DiffCleanupType.NONE]: No cleanup, raw diff output////// The [defaultTextStyle] is applied to unchanged text./// The [addedTextStyle] is applied to text that was added (present in [newText] but not in [oldText])./// The [deletedTextStyle] is applied to text that was deleted (present in [oldText] but not in [newText]).////// The [diffTimeout] limits computation time in seconds. If the mapping phase takes longer than this,/// computation is truncated and returns the best solution found. A value of 0 allows unlimited computation.////// The [diffEditCost] affects cleanup when using [DiffCleanupType.EFFICIENCY]. Higher values result in/// more aggressive cleanup.////// The following properties are passed directly to the underlying [RichText] widget:/// - [textAlign]: How the text should be aligned horizontally./// - [textDirection]: The directionality of the text./// - [softWrap]: Whether the text should break at soft line breaks./// - [overflow]: How visual overflow should be handled./// - [textScaleFactor]: The number of font pixels for each logical pixel./// - [maxLines]: An optional maximum number of lines for the text to span./// - [locale]: Used to select region-specific glyphs and formatting./// - [strutStyle]: Defines the strut, which sets minimum vertical layout metrics./// - [textWidthBasis]: Defines how to measure the width of the rendered text./// - [textHeightBehavior]: Defines how the paragraph will apply TextStyle.height to the ascent of the first line and descent of the last line.
Bring Your Own Diffs
If you'd like to calculate your own diffs, you can do so and pass them via the withDiffs contructor: