Skip to content

add headers for NetworkImage #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class _MyHomePageState extends State<MyHomePage> {
body: SingleChildScrollView(
child: Html(
data: htmlData,
imageHeaders: {"cookie": "kkk"},
//Optional parameters:
style: {
"html": Style(
Expand Down
3 changes: 3 additions & 0 deletions lib/flutter_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Html extends StatelessWidget {
Html({
Key key,
@required this.data,
this.imageHeaders,
this.onLinkTap,
this.customRender,
this.onImageError,
Expand All @@ -45,6 +46,7 @@ class Html extends StatelessWidget {
}) : super(key: key);

final String data;
final Map<String, String> imageHeaders;
final OnTap onLinkTap;
final ImageErrorListener onImageError;
final bool shrinkWrap;
Expand Down Expand Up @@ -74,6 +76,7 @@ class Html extends StatelessWidget {
width: width,
child: HtmlParser(
htmlData: data,
imageHeaders: imageHeaders,
onLinkTap: onLinkTap,
onImageTap: onImageTap,
onImageError: onImageError,
Expand Down
10 changes: 9 additions & 1 deletion lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef CustomRender = Widget Function(

class HtmlParser extends StatelessWidget {
final String htmlData;
final Map<String, String> imageHeaders;
final OnTap onLinkTap;
final OnTap onImageTap;
final ImageErrorListener onImageError;
Expand All @@ -36,6 +37,7 @@ class HtmlParser extends StatelessWidget {

HtmlParser({
@required this.htmlData,
this.imageHeaders,
this.onLinkTap,
this.onImageTap,
this.onImageError,
Expand All @@ -54,6 +56,7 @@ class HtmlParser extends StatelessWidget {
customRender?.keys?.toList() ?? [],
blacklistedElements,
navigationDelegateForIframe,
imageHeaders
);
StyledElement styledTree = applyCSS(lexedTree);
StyledElement inlineStyledTree = applyInlineStyles(styledTree);
Expand Down Expand Up @@ -96,11 +99,13 @@ class HtmlParser extends StatelessWidget {
List<String> customRenderTags,
List<String> blacklistedElements,
NavigationDelegate navigationDelegateForIframe,
Map<String,String> header
) {
StyledElement tree = StyledElement(
name: "[Tree Root]",
children: new List<StyledElement>(),
node: html.documentElement,
map: header
);

html.nodes.forEach((node) {
Expand All @@ -109,6 +114,7 @@ class HtmlParser extends StatelessWidget {
customRenderTags,
blacklistedElements,
navigationDelegateForIframe,
header
));
});

Expand All @@ -124,6 +130,7 @@ class HtmlParser extends StatelessWidget {
List<String> customRenderTags,
List<String> blacklistedElements,
NavigationDelegate navigationDelegateForIframe,
Map <String,String>header
) {
List<StyledElement> children = List<StyledElement>();

Expand All @@ -133,6 +140,7 @@ class HtmlParser extends StatelessWidget {
customRenderTags,
blacklistedElements,
navigationDelegateForIframe,
header
));
});

Expand All @@ -146,7 +154,7 @@ class HtmlParser extends StatelessWidget {
} else if (INTERACTABLE_ELEMENTS.contains(node.localName)) {
return parseInteractableElement(node, children);
} else if (REPLACED_ELEMENTS.contains(node.localName)) {
return parseReplacedElement(node, navigationDelegateForIframe);
return parseReplacedElement(node, navigationDelegateForIframe, header);
} else if (LAYOUT_ELEMENTS.contains(node.localName)) {
return parseLayoutElement(node, children);
} else if (TABLE_CELL_ELEMENTS.contains(node.localName)) {
Expand Down
37 changes: 21 additions & 16 deletions lib/src/replaced_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ class TextContentElement extends ReplacedElement {
/// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img
class ImageContentElement extends ReplacedElement {
final String src;
final Map<String, String> imageHeaders;
final String alt;

ImageContentElement({
String name,
Style style,
this.imageHeaders,
this.src,
this.alt,
dom.Element node,
Expand Down Expand Up @@ -122,14 +124,15 @@ class ImageContentElement extends ReplacedElement {
);
} else {
precacheImage(
NetworkImage(src),
NetworkImage(src, headers: imageHeaders),
context.buildContext,
onError: (exception, StackTrace stackTrace) {
context.parser.onImageError?.call(exception, stackTrace);
},
);
Completer<Size> completer = Completer();
Image image = Image.network(src, frameBuilder: (ctx, child, frame, _) {
Image image = Image.network(src, headers: imageHeaders,
frameBuilder: (ctx, child, frame, _) {
if (frame == null) {
completer.completeError("error");
return child;
Expand All @@ -138,26 +141,27 @@ class ImageContentElement extends ReplacedElement {
}
});
image.image.resolve(ImageConfiguration()).addListener(
ImageStreamListener(
(ImageInfo image, bool synchronousCall) {
var myImage = image.image;
Size size = Size(myImage.width.toDouble(), myImage.height.toDouble());
completer.complete(size);
}, onError: (object, stacktrace) {
completer.completeError(object);
}
),
);
ImageStreamListener((ImageInfo image, bool synchronousCall) {
var myImage = image.image;
Size size =
Size(myImage.width.toDouble(), myImage.height.toDouble());
completer.complete(size);
}, onError: (object, stacktrace) {
completer.completeError(object);
}),
);
imageWidget = FutureBuilder<Size>(
future: completer.future,
builder: (BuildContext buildContext, AsyncSnapshot<Size> snapshot) {
if (snapshot.hasData) {
return new Image.network(
src,
headers: imageHeaders,
width: snapshot.data.width,
frameBuilder: (ctx, child, frame, _) {
if (frame == null) {
return Text(alt ?? "", style: context.style.generateTextStyle());
return Text(alt ?? "",
style: context.style.generateTextStyle());
}
return child;
},
Expand Down Expand Up @@ -392,9 +396,9 @@ class RubyElement extends ReplacedElement {
}

ReplacedElement parseReplacedElement(
dom.Element element,
NavigationDelegate navigationDelegateForIframe,
) {
dom.Element element,
NavigationDelegate navigationDelegateForIframe,
Map<String, String> imageHeaders) {
switch (element.localName) {
case "audio":
final sources = <String>[
Expand Down Expand Up @@ -426,6 +430,7 @@ ReplacedElement parseReplacedElement(
case "img":
return ImageContentElement(
name: "img",
imageHeaders: imageHeaders,
src: element.attributes['src'],
alt: element.attributes['alt'],
node: element,
Expand Down
1 change: 1 addition & 0 deletions lib/src/styled_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class StyledElement {
this.children,
this.style,
dom.Element node,
Map<String ,String> map,
}) : this._node = node;

bool matchesSelector(String selector) =>
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies:

# Plugins for rendering the <video> tag.
video_player: ^1.0.1
chewie: ^0.12.0
chewie: ^0.12.1

# Plugin for rendering the <iframe> tag.
webview_flutter: ^1.0.0
Expand Down
8 changes: 6 additions & 2 deletions test/html_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void testNewParser() {
[],
[],
null,
null,
);
print(tree.toString());
});
Expand All @@ -44,6 +45,7 @@ void testNewParser() {
"Hello, World! <a href='https://example.com'>This is a link</a>"),
[],
[],
null,
null);
print(tree.toString());
});
Expand All @@ -54,6 +56,7 @@ void testNewParser() {
[],
[],
null,
null
);
print(tree.toString());
});
Expand All @@ -65,6 +68,7 @@ void testNewParser() {
[],
[],
null,
null
);
print(tree.toString());
});
Expand All @@ -78,7 +82,7 @@ void testNewParser() {
Your browser does not support the video tag.
</video>
""").getElementsByTagName("video")[0],
null,
null, null
);

expect(videoContentElement, isA<VideoContentElement>());
Expand All @@ -98,7 +102,7 @@ void testNewParser() {
<source src='audio.wav' type='audio/wav'>
Your browser does not support the audio tag.
</audio>
""").getElementsByTagName("audio")[0],
""").getElementsByTagName("audio")[0], null,
null,
);
expect(audioContentElement, isA<AudioContentElement>());
Expand Down