Is there a way to block ads in an flutter WebView? I am building app that lets users browse web pages, but need to block ads. Its basically a custom browser, but I need to get rid of ads.
It’s a Flutter plugin that allows you to incorporate WebView widgets into your Flutter app, to use headless WebViews, or to use In-App browsers. So, what’s the difference between webview_flutter (Official flutter plugin) and flutter_webview_plugin ?
In order to call window.flutter_inappwebview.callHandler (handlerName <String>, ...args) properly, you need to wait and listen to the JavaScript event flutterInAppWebViewPlatformReady. This event will be dispatched as soon as the platform (Android or iOS) is ready to handle the callHandler method.
How to implement: 1 Implement Adblock.java into your Project 2 Create a raw folder in Android Studio ( {projectname}appsrcmainresraw) 3 Add filterlists to raw folder More ...
The InAppWebView widget offers a variety of events! Here’s a few of them: onLoadHttpError: event fired when the WebView main page receives an HTTP error; onConsoleMessage: event fired when the WebView receives a JavaScript console message (such as console.log , console.error , etc.);
navigationDelegate: (NavigationRequest request) { return NavigationDecision.prevent;}
I added that to the Webview to prevent navigation to other screens in case they accidentally click on those ads.
WebView(
initialUrl: widget.url,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
javascriptChannels: <JavascriptChannel>[
_toasterJavascriptChannel(context),
].toSet(),
navigationDelegate: (NavigationRequest request) {
return NavigationDecision.prevent;
},
onPageStarted: (String url) {
print('Page started loading: $url');
},
onPageFinished: (String url) {
print('Page finished loading: $url');
},
gestureNavigationEnabled: true,
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With