perf(chat): reuse unchanged markdown render trees
This commit is contained in:
@@ -32,6 +32,24 @@ Widget chatMarkdownText(
|
||||
|
||||
class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
final Map<String, TapGestureRecognizer> _links = {};
|
||||
Widget? _rendered;
|
||||
|
||||
@override
|
||||
void didUpdateWidget(ChatMarkdownView oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.text != oldWidget.text ||
|
||||
(widget.onOpenLink == null) != (oldWidget.onOpenLink == null)) {
|
||||
_rendered = null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
// Cached spans use host theme/localization roles. Never freeze those when
|
||||
// brightness, typography, locale or surrounding text styles change.
|
||||
_rendered = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
@@ -43,9 +61,12 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// A new token or usage update elsewhere in the turn must not reparse and
|
||||
// rebuild every earlier answer part, or disturb its text selection.
|
||||
if (_rendered != null) return _rendered!;
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
return _selectable(
|
||||
return _rendered = _selectable(
|
||||
context,
|
||||
Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
@@ -279,7 +300,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
return TextSpan(children: children);
|
||||
}
|
||||
final recognizer = _links.putIfAbsent(url, TapGestureRecognizer.new);
|
||||
recognizer.onTap = () => onOpenLink(url);
|
||||
recognizer.onTap = () => widget.onOpenLink?.call(url);
|
||||
return _withRecognizer(
|
||||
TextSpan(
|
||||
style: const TextStyle(decoration: TextDecoration.underline),
|
||||
|
||||
Reference in New Issue
Block a user