How to inject a simple code in Flutter WebView to hide a part of the website (footer)?
You can try
flutterWebviewPlugin.evalJavascript('alert("Hello World")')
Remember that the evalJavascript()
expects the JS
not HTML
, So you can't use like
flutterWebviewPlugin.evalJavascript('<script language="JavaScript" type="text/javascript">alert("Hello World")</script>')
Here is the complete example for your reference,
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
class JSInWebView extends StatefulWidget {
@override
JSInWebViewState createState() {
return new JSInWebViewState();
}
}
class JSInWebViewState extends State<JSInWebView> {
final flutterWebviewPlugin = new FlutterWebviewPlugin();
// alternatively you can define variable as var js = "YOUR_SCRIPT"; and use it inside evalJavascript
@override
void initState(){
super.initState();
flutterWebviewPlugin.evalJavascript("alert('Hi, I just executed')");
}
@override
void dispose() {
flutterWebviewPlugin.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return WebviewScaffold(
url: 'https://google.com',
hidden: true,
appBar: AppBar(title: Text("Elite")),
);
}
}