1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
import 'dart:math';
import 'package:aves/utils/diff_match.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
class AnimatedDiffText extends StatefulWidget {
final String text;
final TextStyle? textStyle;
final StrutStyle? strutStyle;
final Curve curve;
final Duration duration;
const AnimatedDiffText(
this.text, {
super.key,
this.textStyle,
this.strutStyle,
this.curve = Curves.easeInOutCubic,
required this.duration,
});
@override
State<AnimatedDiffText> createState() => _AnimatedDiffTextState();
}
class _AnimatedDiffTextState extends State<AnimatedDiffText> with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final CurvedAnimation _animation;
final List<_TextDiff> _diffs = [];
TextStyle get _textStyle {
final style = widget.textStyle ?? const TextStyle();
// specify `height` for accurate paragraph height measurement
return style.copyWith(height: style.height ?? DefaultTextStyle.of(context).style.height);
}
StrutStyle? get _strutStyle => widget.strutStyle;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: widget.duration,
vsync: this,
);
_animation = CurvedAnimation(parent: _controller, curve: widget.curve);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_computeDiff(widget.text, widget.text);
}
@override
void didUpdateWidget(covariant AnimatedDiffText oldWidget) {
super.didUpdateWidget(oldWidget);
final oldText = oldWidget.text;
final newText = widget.text;
if (oldText != newText) {
_computeDiff(oldText, newText);
_controller.forward(from: 0);
}
}
@override
void dispose() {
_animation.dispose();
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Text.rich(
TextSpan(
children: _diffs.map((diff) {
final (oldText, newText, oldSize, newSize) = diff;
final text = (_animation.value == 0 ? oldText : newText) ?? '';
return WidgetSpan(
child: AnimatedSize(
key: ValueKey(diff),
curve: widget.curve,
duration: widget.duration,
child: AnimatedSwitcher(
duration: widget.duration,
switchInCurve: widget.curve,
switchOutCurve: widget.curve,
layoutBuilder: (currentChild, previousChildren) {
return Stack(
alignment: Alignment.center,
children: [
...previousChildren.map(
(child) => ConstrainedBox(
constraints: BoxConstraints.tight(Size(
min(oldSize.width, newSize.width),
min(oldSize.height, newSize.height),
)),
child: child,
),
),
if (currentChild != null) currentChild,
],
);
},
child: Text(
text,
key: Key(text),
style: _textStyle,
),
),
),
);
}).toList(),
),
strutStyle: _strutStyle,
);
},
);
}
Size _textSize(String text) {
final paragraph = RenderParagraph(
TextSpan(text: text, style: _textStyle),
textDirection: Directionality.of(context),
textScaler: MediaQuery.textScalerOf(context),
strutStyle: _strutStyle,
)..layout(const BoxConstraints(), parentUsesSize: true);
final width = paragraph.getMaxIntrinsicWidth(double.infinity);
final height = paragraph.getMaxIntrinsicHeight(double.infinity);
paragraph.dispose();
return Size(width, height);
}
// use an adaptation of Google's `Diff Match and Patch`
// as package `diffutil_dart` (as of v3.0.0) is unreliable
void _computeDiff(String oldText, String newText) {
final oldCharacters = oldText.characters.join();
final newCharacters = newText.characters.join();
final dmp = DiffMatchPatch();
final d = dmp.diff_main(oldCharacters, newCharacters);
dmp.diff_cleanupSemantic(d);
_diffs
..clear()
..addAll(d.map((diff) {
final text = diff.text;
final size = _textSize(text);
return switch (diff.operation) {
Operation.delete => (text, null, size, Size.zero),
Operation.insert => (null, text, Size.zero, size),
Operation.equal || _ => (text, text, size, size),
};
}).fold<List<_TextDiff>>([], (prev, v) {
if (prev.isNotEmpty) {
final last = prev.last;
final prevNewText = last.$2;
if (prevNewText == null) {
// previous diff is a deletion
final thisOldText = v.$1;
if (thisOldText == null) {
// this diff is an insertion
// merge deletion and insertion as a change operation
final change = (last.$1, v.$2, last.$3, v.$4);
return [...prev.take(prev.length - 1), change];
}
}
}
return [...prev, v];
}));
}
}
typedef _TextDiff = (String?, String?, Size, Size);
|