From a6f0ed3764298ad40d6d938e81dd4d0aee860f39 Mon Sep 17 00:00:00 2001 From: Whiterzi Date: Mon, 20 Oct 2025 14:36:57 +0800 Subject: [PATCH] #20591: prevent applying extra \n after last inline block --- packages/flutter_markdown/lib/src/builder.dart | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/flutter_markdown/lib/src/builder.dart b/packages/flutter_markdown/lib/src/builder.dart index bc58504af1f8..f3ef555c2a74 100644 --- a/packages/flutter_markdown/lib/src/builder.dart +++ b/packages/flutter_markdown/lib/src/builder.dart @@ -261,6 +261,8 @@ class MarkdownBuilder implements md.NodeVisitor { String? _lastVisitedTag; bool _isInBlockquote = false; + bool _isBuildingLastNode = false; + /// Returns widgets that display the given Markdown nodes. /// /// The returned widgets are typically used as children in a [ListView]. @@ -279,9 +281,10 @@ class MarkdownBuilder implements md.NodeVisitor { }); _blocks.add(_BlockElement(null)); - + _isBuildingLastNode = false; for (final md.Node node in nodes) { assert(_blocks.length == 1); + _isBuildingLastNode = node == nodes.last; //Not allow ul in p case //https://stackoverflow.com/a/5681796 if (node is md.Element && node.tag == 'p') { @@ -712,6 +715,8 @@ class MarkdownBuilder implements md.NodeVisitor { child = child0; if (isInlineBlock) { + // #20591: prevent applying extra \n for the last inline block + if (!_isBuildingLastNode) { _inlineWidgets.add(_buildRichText(TextSpan( // see another similar block in the visitElementBefore // since we don't know if the following block is a inline text block, @@ -719,6 +724,7 @@ class MarkdownBuilder implements md.NodeVisitor { text: '\n', style: styleSheet.styles[tag] ))); + } } else { _addBlockChild(child); }