问题
I try to insert the next code(Google AdSense) at my site, but unsuccessful by now. The code is:
<script type="text/javascript">var nend_params={"media":00000,"site":000000,"spot":000000,"type":1,"oriented":1};0</script>
<script type="text/javascript" src="https://js1.nend.net/js/nendAdLoader.js"></script>
1.For a while I tried a 'dart:js' with sentences like that
js.context.callMethod(..
but, think its not worked, because we need to show advertising, not only execute javascript code..
2.next, I suppose that it can be a WebView (a webview, at the webapp..) and try to use next:
dependencies: webview_flutter: ^0.3.14 but it is fall with
"Error: PlatformException(Unregistered factory, No factory registered for viewtype 'plugins.flutter.io/webview'"...
May be someone heve a positive experiense or can give me any advice.
回答1:
If you just need those scripts in your webpage you could try to include it in the web/Index.html
page directly.
Otherwise you will have to to use ui.platformViewRegistry.registerViewFactory
as shown here PlatFormView example. Following code would add a script element to your page.
import 'package:flutter/material.dart';
import 'dart:ui' as ui;
import 'dart:html';
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
ui.platformViewRegistry.registerViewFactory("add_script", (int viewId) {
ScriptElement element = ScriptElement()
..src = "https://js1.nend.net/js/nendAdLoader.js"
..type = "text/javascript";
return element;
});
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Directionality(
textDirection: TextDirection.ltr,
child: SizedBox(
width: 640,
height: 360,
child: HtmlElementView(viewType: 'add_script'),
),
),
],
),
);
}
}
Note: This code needs a hosting widget like a scaffold inside a MaterialApp or something similar. For the code inside the script try creating a similar widget with different viewtype string and use the
setInnerHtml
or similar methods.
But you should be aware that this element is added as a shadow dom element in your page. This issue discusses about this.
来源:https://stackoverflow.com/questions/57909791/is-it-possible-to-insert-google-adsense-at-flutter-web-application