[flutter_markdown] Unable to use MarkdownElementBuilder to act those tags without children. (#3952)

fix https://github.com/flutter/flutter/issues/126402

- [  ] I updated `pubspec.yaml` with an appropriate new version according to the [pub versioning philosophy], or this PR is [exempt from version changes].
- [  ] I updated `CHANGELOG.md` to add a description of the change, [following repository CHANGELOG style].
- [  ] I updated/added relevant documentation (doc comments with `///`).
- [  ] I added new tests to check the change I am making, or this PR is [test-exempt].
- [  ] All existing and new tests are passing.
This commit is contained in:
杨加康
2023-05-31 04:51:53 +08:00
committed by GitHub
parent e942e40c1c
commit 0f293a57a4
4 changed files with 39 additions and 3 deletions

View File

@ -1,9 +1,10 @@
## NEXT ## 0.6.15
* Fixes unawaited_futures violations. * Fixes unawaited_futures violations.
* Updates minimum Flutter version to 3.3. * Updates minimum Flutter version to 3.3.
* Aligns Dart and Flutter SDK constraints. * Aligns Dart and Flutter SDK constraints.
* Replace `describeEnum` with the `name` getter. * Replace `describeEnum` with the `name` getter.
* Supports custom rendering of tags without children.
## 0.6.14 ## 0.6.14

View File

@ -454,7 +454,11 @@ class MarkdownBuilder implements md.NodeVisitor {
final Widget? child = final Widget? child =
builders[tag]!.visitElementAfter(element, styleSheet.styles[tag]); builders[tag]!.visitElementAfter(element, styleSheet.styles[tag]);
if (child != null) { if (child != null) {
current.children[0] = child; if (current.children.isEmpty) {
current.children.add(child);
} else {
current.children[0] = child;
}
} }
} else if (tag == 'img') { } else if (tag == 'img') {
// create an image widget for this image // create an image widget for this image

View File

@ -4,7 +4,7 @@ description: A Markdown renderer for Flutter. Create rich text output,
formatted with simple Markdown tags. formatted with simple Markdown tags.
repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22 issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
version: 0.6.14 version: 0.6.15
environment: environment:
sdk: ">=2.18.0 <4.0.0" sdk: ">=2.18.0 <4.0.0"

View File

@ -112,6 +112,30 @@ void defineTests() {
expect(widgetSpan.child, isInstanceOf<Container>()); expect(widgetSpan.child, isInstanceOf<Container>());
}, },
); );
testWidgets(
'Custom rendering of tags without children',
(WidgetTester tester) async {
const String data = '![alt](/assets/images/logo.png)';
await tester.pumpWidget(
boilerplate(
Markdown(
data: data,
builders: <String, MarkdownElementBuilder>{
'img': ImgBuilder(),
},
),
),
);
final Finder imageFinder = find.byType(Image);
expect(imageFinder, findsNothing);
final Finder textFinder = find.byType(Text);
expect(textFinder, findsOneWidget);
final Text textWidget = tester.widget(find.byType(Text));
expect(textWidget.data, 'foo');
},
);
} }
class SubscriptSyntax extends md.InlineSyntax { class SubscriptSyntax extends md.InlineSyntax {
@ -225,3 +249,10 @@ class ContainerBuilder2 extends MarkdownElementBuilder {
); );
} }
} }
class ImgBuilder extends MarkdownElementBuilder {
@override
Widget? visitElementAfter(md.Element element, TextStyle? preferredStyle) {
return Text('foo', style: preferredStyle);
}
}