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

pretty_diff_text 封面
插件详情v2.1.0

pretty_diff_text

PrettyDiffText 是 RichText 的包装器,可视觉化显示两段文本之间的差异。它默认使用 Google 的 diff-match-patch 库。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.7万
喜欢数
66
Pub 分数
160

安装插件

flutter pub add pretty_diff_text

插件介绍

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

PrettyDiffText 是一个 Flutter 包,它包装了 RichText 以视觉方式突出显示两个文本字符串之间的差异。它利用 Google 的 diff-match-patch 库进行准确高效的文本比较,非常适合以可读格式展示编辑、更改或修订。该包支持多个平台,并可无缝集成到需要实时或历史文本比较的应用程序中。通过颜色和格式清晰标记添加、删除和修改内容,从而提升用户体验。

适合场景

  • 在协作应用中显示文本修订
  • 可视化代码或文档更改
  • 在笔记应用中显示编辑历史
  • 突出显示版本化内容的差异
  • 实时聊天或消息中的文本对比
  • 跟踪配置或设置的更新

主要特点

  • 使用 diff-match-patch 进行精确比较
  • 支持富文本格式
  • 颜色编码的添加和删除
  • 跨平台兼容性
  • 与现有 RichText 小部件的轻松集成
  • 可自定义的样式选项

适合对象

  • 开发编辑或协作工具的开发者
  • 需要更改跟踪的应用程序
  • 教育或文档平台
  • 版本控制接口
  • 显示动态文本更新的 UI

使用建议

  • 可能增加简单情况下的组件复杂性
  • 长文本带来的性能影响
  • 需要仔细处理嵌套的富文本
  • 超出基础样式的有限自定义
  • 依赖外部的 diff-match-patch 库
  • 未针对高频更新进行优化

原始 README

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

PrettyDiffText

Snapshot

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.
    • All customization which Flutter's RichText has.

Installing:

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

dependencies:
  pretty_diff_text: ^2.1.0

Import

import 'package:pretty_diff_text/pretty_diff_text.dart';

How to use

It is easy as using Text widget!

PrettyDiffText(
  oldText: "Your old text",
  newText: "Your new text",
  )

Snapshot

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:

PrettyDiffText.withDiffs(
  diffs: _getDiffs(
    oldText: _oldTextEditingController.text,
    newText: _newTextEditingController.text,
    diffCleanupType: _diffCleanupType,
    diffTimeout: _diffTimeout,
    diffEditCost: _diffEditCost,
  ),
)

Demo Apps

Clone the demo app to play around: DEMO

Changelog

CHANGELOG

License

MIT License

References:

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目