Visitar URL original
Merge pull request #52 from DavBfr/master · stuartmorgan-g/packages@f5ed6df · GitHub
Skip to content

Commit f5ed6df

Browse files
authored
Merge pull request flutter#52 from DavBfr/master
Add support for asset images
2 parents 9ed2ffc + 6755c42 commit f5ed6df

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎packages/flutter_markdown/README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ design, use the MarkdownRaw class.
2626

2727
## Image support
2828

29-
The `Img` tag only supports the following image locations. It specifically
30-
does not support image locations referring to bundled assets.
29+
The `Img` tag only supports the following image locations:
3130

3231
* From the network: Use a URL prefixed by either `http://` or `https://`.
3332

3433
* From local files on the device: Use an absolute path to the file, for example by
3534
concatenating the file name with the path returned by a known storage location,
3635
such as those provided by the [`path_provider`](https://pub.dartlang.org/packages/path_provider)
3736
plugin.
37+
38+
* From image locations referring to bundled assets: Use an asset name prefixed by `resource:`.
39+
like `resource:assets/image.png`.

‎packages/flutter_markdown/lib/src/builder.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ class MarkdownBuilder implements md.NodeVisitor {
262262
child = new Image.network(uri.toString(), width: width, height: height);
263263
} else if (uri.scheme == 'data') {
264264
child = _handleDataSchemeUri(uri, width, height);
265+
} else if (uri.scheme == "resource") {
266+
child = new Image.asset(path.substring(9), width: width, height: height);
265267
} else {
266268
String filePath = (imageDirectory == null
267269
? uri.toFilePath()

‎packages/flutter_markdown/test/flutter_markdown_test.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ void main() {
217217
expect(image.image is FileImage, isTrue);
218218
});
219219

220+
testWidgets('should work with resources', (WidgetTester tester) async {
221+
await tester.pumpWidget(_boilerplate(
222+
const Markdown(data: '![alt](resource:assets/logo.png)')));
223+
224+
final Image image =
225+
tester.allWidgets.firstWhere((Widget widget) => widget is Image);
226+
expect(image.image is AssetImage, isTrue);
227+
expect((image.image as AssetImage).assetName == 'assets/logo.png', isTrue);
228+
});
229+
220230
testWidgets('should work with local image files', (WidgetTester tester) async {
221231
await tester
222232
.pumpWidget(_boilerplate(const Markdown(data: '![alt](img.png#50x50)')));

0 commit comments

Comments
 (0)