From 3ea7b8ed319806da27ac4e87376cdf9528d36907 Mon Sep 17 00:00:00 2001 From: Mikhail Matiunin Date: Thu, 9 Oct 2025 18:16:26 +0400 Subject: [PATCH] Invalidate and relayout render object after system fonts changed --- CHANGELOG.md | 6 +++++- lib/src/render.dart | 26 +++++++++++++++++++++++++- pubspec.yaml | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64e4d9e..e993711 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.8 + +- **FIXED**: Invalidate and relayout render object after system fonts changed. + ## 0.0.7 - **FIXED**: Preserved indentation on line breaks within list items [#4]. @@ -7,7 +11,7 @@ ## 0.0.6 -- **FIXED**: Fixed escaping of special characters. [#6] +- **FIXED**: Fixed escaping of special characters. [#6] ## 0.0.5 diff --git a/lib/src/render.dart b/lib/src/render.dart index 72d93e1..957dd6b 100644 --- a/lib/src/render.dart +++ b/lib/src/render.dart @@ -98,11 +98,19 @@ class MarkdownRenderObject extends RenderBox { _painter.handleEvent(event); } + /// Handles system font changes by marking the render object as needing layout + void _handleSystemFontsChange() { + // Invalidate cached layouts in painter and all block painters + _painter.invalidateLayout(); + // Request new layout and paint + markNeedsLayout(); + } + @override // ignore: unnecessary_overrides void attach(PipelineOwner owner) { super.attach(owner); - // Ensure the painter is mounted when the render object is attached. + PaintingBinding.instance.systemFonts.addListener(_handleSystemFontsChange); } /// Updates the render object with a new values. @@ -124,6 +132,8 @@ class MarkdownRenderObject extends RenderBox { @override @protected void detach() { + PaintingBinding.instance.systemFonts + .removeListener(_handleSystemFontsChange); super.detach(); } @@ -264,6 +274,20 @@ class MarkdownPainter { return true; // Indicate that the painter was updated. } + /// Invalidate cached layouts when system fonts change. + /// This forces TextPainters to recreate their layouts with new fonts. + void invalidateLayout() { + _needsLayout = true; + _lastSize = null; + _lastPicture = null; + // Dispose and rebuild all block painters to recreate TextPainters + // with the new system fonts + for (final painter in _blockPainters) { + painter.dispose(); + } + _rebuild(); + } + /// Layouts the markdown content with the given width. Size layout({required double maxWidth}) { if (_isEmpty) { diff --git a/pubspec.yaml b/pubspec.yaml index b7abed8..3be30af 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,7 +8,7 @@ description: > Markdown library written in Dart. It can parse and display Markdown. -version: 0.0.7 +version: 0.0.8 homepage: https://github.com/DoctorinaAI/md repository: https://github.com/DoctorinaAI/md