Skip to content

Commit 3cfb18e

Browse files
authored
Update README.md
1 parent f98bb5e commit 3cfb18e

File tree

1 file changed

+23
-39
lines changed

1 file changed

+23
-39
lines changed

README.md

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ Widget html = Html(
220220

221221
2. Complex example - rendering an `iframe` differently based on whether it is an embedded youtube video or some other embedded content
222222

223-
Packages used: [`data_connection_checker`](https://pub.dev/packages/data_connection_checker) and [`flutter_inappwebview`](https://pub.dev/packages/flutter_inappwebview)
224-
225223
<details><summary>View code</summary>
226224

227225
```dart
@@ -241,43 +239,29 @@ Widget html = Html(
241239
return Container(
242240
width: width ?? (height ?? 150) * 2,
243241
height: height ?? (width ?? 300) / 2,
244-
child: InAppWebView(
245-
initialUrl: attributes['src'],
246-
// recommended options when using this implementation
247-
initialOptions: InAppWebViewGroupOptions(
248-
crossPlatform: InAppWebViewOptions(
249-
javaScriptEnabled: true,
250-
cacheEnabled: false,
251-
disableVerticalScroll: attributes['src'].contains("youtube.com/embed") ? true : false,
252-
disableHorizontalScroll: attributes['src'].contains("youtube.com/embed") ? true : false,
253-
useShouldOverrideUrlLoading: true,
254-
),
255-
ios: IOSInAppWebViewOptions(
256-
allowsLinkPreview: false,
257-
),
258-
android: AndroidInAppWebViewOptions(
259-
useHybridComposition: true,
260-
)
261-
),
262-
// no need for a scrolling gesture recognizer for embedded youtube videos so we only use VerticalDragGestureRecognizer when the iframe does not display embedded youtube videos
263-
gestureRecognizers: attributes['src'].contains("youtube.com/embed") ? null : [
264-
Factory(() => VerticalDragGestureRecognizer())
265-
].toSet(),
266-
// no need to load other urls when displaying embedded youtube videos so we block url loading requests when this is the case
267-
shouldOverrideUrlLoading: (controller, request) async {
268-
if (attributes['src'].contains("youtube.com/embed")) {
269-
if (!request.url.contains("youtube.com/embed")) {
270-
return ShouldOverrideUrlLoadingAction.CANCEL;
271-
} else {
272-
return ShouldOverrideUrlLoadingAction.ALLOW;
273-
}
274-
} else {
275-
return ShouldOverrideUrlLoadingAction.ALLOW;
276-
}
277-
},
278-
),
279-
);
280-
// if the src of the iframe is null then do not render anything
242+
child: WebView(
243+
initialUrl: attributes['src'],
244+
javascriptMode: JavascriptMode.unrestricted,
245+
//no need for scrolling gesture recognizers on embedded youtube, so set gestureRecognizers null
246+
//on other iframe content scrolling might be necessary, so use VerticalDragGestureRecognizer
247+
gestureRecognizers: attributes['src'].contains("youtube.com/embed") ? null : [
248+
Factory(() => VerticalDragGestureRecognizer())
249+
].toSet(),
250+
navigationDelegate: (NavigationRequest request) async {
251+
//no need to load any url besides the embedded youtube url when displaying embedded youtube, so prevent url loading
252+
//on other iframe content allow all url loading
253+
if (attributes['src'].contains("youtube.com/embed")) {
254+
if (!request.url.contains("youtube.com/embed")) {
255+
return NavigationDecision.prevent;
256+
} else {
257+
return NavigationDecision.navigate;
258+
}
259+
} else {
260+
return NavigationDecision.navigate;
261+
}
262+
},
263+
),
264+
);
281265
} else {
282266
return Container(height: 0);
283267
}

0 commit comments

Comments
 (0)