Skip to content

Commit 403a644

Browse files
committed
Add srcdoc check when building IframeWidget
This branch allows developers to, in addition to 'src', pass in html that contains a 'srcdoc' property. 'src' is great for passing in a URL, but if someone wants to pass in HTML, etc., inside an iframe, then we need to use 'srcdoc'. This branch is broken into three commits since there are three separate features being introduced. Check is 'srcdoc' is sent in with the extensionContext attributes. If there is no 'srcdoc' we fallback to 'src'. This change doesn't break the current usage of the package and will not impact developers currently using it.
1 parent 79ec194 commit 403a644

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

packages/flutter_html_iframe/lib/iframe_mobile.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
import 'package:flutter/foundation.dart';
24
import 'package:flutter/gestures.dart';
35
import 'package:flutter/material.dart';
@@ -34,17 +36,26 @@ class IframeWidget extends StatelessWidget {
3436
final givenHeight =
3537
double.tryParse(extensionContext.attributes['height'] ?? "");
3638

39+
Uri? srcUri;
40+
41+
if (extensionContext.attributes['srcdoc'] != null) {
42+
srcUri = Uri.dataFromString(
43+
extensionContext.attributes['srcdoc'] ?? '',
44+
mimeType: 'text/html',
45+
encoding: Encoding.getByName('utf-8'),
46+
);
47+
} else {
48+
srcUri = Uri.tryParse(extensionContext.attributes['src'] ?? "") ?? Uri();
49+
}
50+
3751
return SizedBox(
3852
width: givenWidth ?? (givenHeight ?? 150) * 2,
3953
height: givenHeight ?? (givenWidth ?? 300) / 2,
4054
child: CssBoxWidget(
4155
style: extensionContext.styledElement!.style,
4256
childIsReplaced: true,
4357
child: WebViewWidget(
44-
controller: controller
45-
..loadRequest(
46-
Uri.tryParse(extensionContext.attributes['src'] ?? "") ??
47-
Uri()),
58+
controller: controller..loadRequest(srcUri),
4859
key: key,
4960
gestureRecognizers: {Factory(() => VerticalDragGestureRecognizer())},
5061
),

0 commit comments

Comments
 (0)