diff --git a/packages/flutter_markdown/README.md b/packages/flutter_markdown/README.md index 05bb2662cb..35c4a26790 100644 --- a/packages/flutter_markdown/README.md +++ b/packages/flutter_markdown/README.md @@ -26,8 +26,7 @@ design, use the MarkdownRaw class. ## Image support -The `Img` tag only supports the following image locations. It specifically -does not support image locations referring to bundled assets. +The `Img` tag only supports the following image locations: * From the network: Use a URL prefixed by either `http://` or `https://`. @@ -35,3 +34,6 @@ does not support image locations referring to bundled assets. concatenating the file name with the path returned by a known storage location, such as those provided by the [`path_provider`](https://pub.dartlang.org/packages/path_provider) plugin. + +* From image locations referring to bundled assets: Use an asset name prefixed by `resource:`. + like `resource:assets/image.png`. diff --git a/packages/flutter_markdown/lib/src/builder.dart b/packages/flutter_markdown/lib/src/builder.dart index 059bbadbe8..7f462edc29 100644 --- a/packages/flutter_markdown/lib/src/builder.dart +++ b/packages/flutter_markdown/lib/src/builder.dart @@ -262,6 +262,8 @@ class MarkdownBuilder implements md.NodeVisitor { child = new Image.network(uri.toString(), width: width, height: height); } else if (uri.scheme == 'data') { child = _handleDataSchemeUri(uri, width, height); + } else if (uri.scheme == "resource") { + child = new Image.asset(path.substring(9), width: width, height: height); } else { String filePath = (imageDirectory == null ? uri.toFilePath() diff --git a/packages/flutter_markdown/test/flutter_markdown_test.dart b/packages/flutter_markdown/test/flutter_markdown_test.dart index ca341f622d..7b0989a37c 100644 --- a/packages/flutter_markdown/test/flutter_markdown_test.dart +++ b/packages/flutter_markdown/test/flutter_markdown_test.dart @@ -217,6 +217,16 @@ void main() { expect(image.image is FileImage, isTrue); }); + testWidgets('should work with resources', (WidgetTester tester) async { + await tester.pumpWidget(_boilerplate( + const Markdown(data: '![alt](resource:assets/logo.png)'))); + + final Image image = + tester.allWidgets.firstWhere((Widget widget) => widget is Image); + expect(image.image is AssetImage, isTrue); + expect((image.image as AssetImage).assetName == 'assets/logo.png', isTrue); + }); + testWidgets('should work with local image files', (WidgetTester tester) async { await tester .pumpWidget(_boilerplate(const Markdown(data: '![alt](img.png#50x50)')));