|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:convert'; |
| 3 | + |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_html/html_parser.dart'; |
| 6 | +import 'package:flutter_svg/flutter_svg.dart'; |
| 7 | +import 'package:html/dom.dart' as dom; |
| 8 | + |
| 9 | +typedef ImageSourceMatcher = bool Function( |
| 10 | + Map<String, String> attributes, |
| 11 | + dom.Element element, |
| 12 | +); |
| 13 | + |
| 14 | +ImageSourceMatcher base64DataUriMatcher() => (attributes, element) => |
| 15 | + _src(attributes) != null && |
| 16 | + _src(attributes).startsWith("data:image") && |
| 17 | + _src(attributes).contains("base64,"); |
| 18 | + |
| 19 | +ImageSourceMatcher networkSourceMatcher({ |
| 20 | + List<String> schemas: const ["https", "http"], |
| 21 | + List<String> domains, |
| 22 | + String extension, |
| 23 | +}) => |
| 24 | + (attributes, element) { |
| 25 | + if (_src(attributes) == null) return false; |
| 26 | + try { |
| 27 | + final src = Uri.parse(_src(attributes)); |
| 28 | + return schemas.contains(src.scheme) && |
| 29 | + (domains == null || domains.contains(src.host)) && |
| 30 | + (extension == null || src.path.endsWith(".$extension")); |
| 31 | + } catch (e) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | +ImageSourceMatcher assetUriMatcher() => (attributes, element) => |
| 37 | + _src(attributes) != null && _src(attributes).startsWith("asset:"); |
| 38 | + |
| 39 | +typedef ImageRender = Widget Function( |
| 40 | + RenderContext context, |
| 41 | + Map<String, String> attributes, |
| 42 | + dom.Element element, |
| 43 | +); |
| 44 | + |
| 45 | +ImageRender base64ImageRender() => (context, attributes, element) { |
| 46 | + final decodedImage = |
| 47 | + base64.decode(_src(attributes).split("base64,")[1].trim()); |
| 48 | + precacheImage( |
| 49 | + MemoryImage(decodedImage), |
| 50 | + context.buildContext, |
| 51 | + onError: (exception, StackTrace stackTrace) { |
| 52 | + context.parser.onImageError?.call(exception, stackTrace); |
| 53 | + }, |
| 54 | + ); |
| 55 | + return Image.memory( |
| 56 | + decodedImage, |
| 57 | + frameBuilder: (ctx, child, frame, _) { |
| 58 | + if (frame == null) { |
| 59 | + return Text(_alt(attributes) ?? "", |
| 60 | + style: context.style.generateTextStyle()); |
| 61 | + } |
| 62 | + return child; |
| 63 | + }, |
| 64 | + ); |
| 65 | + }; |
| 66 | + |
| 67 | +ImageRender assetImageRender({ |
| 68 | + double width, |
| 69 | + double height, |
| 70 | +}) => |
| 71 | + (context, attributes, element) { |
| 72 | + final assetPath = _src(attributes).replaceFirst('asset:', ''); |
| 73 | + if (_src(attributes).endsWith(".svg")) { |
| 74 | + return SvgPicture.asset(assetPath); |
| 75 | + } else { |
| 76 | + return Image.asset( |
| 77 | + assetPath, |
| 78 | + width: width ?? _width(attributes), |
| 79 | + height: height ?? _height(attributes), |
| 80 | + frameBuilder: (ctx, child, frame, _) { |
| 81 | + if (frame == null) { |
| 82 | + return Text(_alt(attributes) ?? "", |
| 83 | + style: context.style.generateTextStyle()); |
| 84 | + } |
| 85 | + return child; |
| 86 | + }, |
| 87 | + ); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | +ImageRender networkImageRender({ |
| 92 | + Map<String, String> headers, |
| 93 | + double width, |
| 94 | + double height, |
| 95 | + Widget Function(String) altWidget, |
| 96 | +}) => |
| 97 | + (context, attributes, element) { |
| 98 | + precacheImage( |
| 99 | + NetworkImage( |
| 100 | + _src(attributes), |
| 101 | + headers: headers, |
| 102 | + ), |
| 103 | + context.buildContext, |
| 104 | + onError: (exception, StackTrace stackTrace) { |
| 105 | + context.parser.onImageError?.call(exception, stackTrace); |
| 106 | + }, |
| 107 | + ); |
| 108 | + Completer<Size> completer = Completer(); |
| 109 | + Image image = |
| 110 | + Image.network(_src(attributes), frameBuilder: (ctx, child, frame, _) { |
| 111 | + if (frame == null) { |
| 112 | + if (!completer.isCompleted) { |
| 113 | + completer.completeError("error"); |
| 114 | + } |
| 115 | + return child; |
| 116 | + } else { |
| 117 | + return child; |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + image.image.resolve(ImageConfiguration()).addListener( |
| 122 | + ImageStreamListener((ImageInfo image, bool synchronousCall) { |
| 123 | + var myImage = image.image; |
| 124 | + Size size = |
| 125 | + Size(myImage.width.toDouble(), myImage.height.toDouble()); |
| 126 | + if (!completer.isCompleted) { |
| 127 | + completer.complete(size); |
| 128 | + } |
| 129 | + }, onError: (object, stacktrace) { |
| 130 | + if (!completer.isCompleted) { |
| 131 | + completer.completeError(object); |
| 132 | + } |
| 133 | + }), |
| 134 | + ); |
| 135 | + return FutureBuilder<Size>( |
| 136 | + future: completer.future, |
| 137 | + builder: (BuildContext buildContext, AsyncSnapshot<Size> snapshot) { |
| 138 | + if (snapshot.hasData) { |
| 139 | + return Image.network( |
| 140 | + _src(attributes), |
| 141 | + headers: headers, |
| 142 | + width: width ?? _width(attributes) ?? snapshot.data.width, |
| 143 | + height: height ?? _height(attributes), |
| 144 | + frameBuilder: (ctx, child, frame, _) { |
| 145 | + if (frame == null) { |
| 146 | + return altWidget.call(_alt(attributes)) ?? |
| 147 | + Text(_alt(attributes) ?? "", |
| 148 | + style: context.style.generateTextStyle()); |
| 149 | + } |
| 150 | + return child; |
| 151 | + }, |
| 152 | + ); |
| 153 | + } else if (snapshot.hasError) { |
| 154 | + return Text(_alt(attributes) ?? "", |
| 155 | + style: context.style.generateTextStyle()); |
| 156 | + } else { |
| 157 | + return new CircularProgressIndicator(); |
| 158 | + } |
| 159 | + }, |
| 160 | + ); |
| 161 | + }; |
| 162 | + |
| 163 | +ImageRender svgNetworkImageRender() => (context, attributes, element) { |
| 164 | + return SvgPicture.network(attributes["src"]); |
| 165 | + }; |
| 166 | + |
| 167 | +final Map<ImageSourceMatcher, ImageRender> defaultImageRenders = { |
| 168 | + base64DataUriMatcher(): base64ImageRender(), |
| 169 | + assetUriMatcher(): assetImageRender(), |
| 170 | + networkSourceMatcher(extension: "svg"): svgNetworkImageRender(), |
| 171 | + networkSourceMatcher(): networkImageRender(), |
| 172 | +}; |
| 173 | + |
| 174 | +String _src(Map<String, String> attributes) { |
| 175 | + return attributes["src"]; |
| 176 | +} |
| 177 | + |
| 178 | +String _alt(Map<String, String> attributes) { |
| 179 | + return attributes["alt"]; |
| 180 | +} |
| 181 | + |
| 182 | +double _height(Map<String, String> attributes) { |
| 183 | + final heightString = attributes["height"]; |
| 184 | + return heightString == null ? heightString : double.tryParse(heightString); |
| 185 | +} |
| 186 | + |
| 187 | +double _width(Map<String, String> attributes) { |
| 188 | + final widthString = attributes["width"]; |
| 189 | + return widthString == null ? widthString : double.tryParse(widthString); |
| 190 | +} |
0 commit comments