是否可以-在Flutter Web应用程序中插入Google AdSense? [英] is it possible - to insert google AdSense at Flutter Web application?
问题描述
我尝试在我的网站上插入下一个代码(Google AdSense),但目前未成功。代码为:
< script type = text / javascript> var nend_params = { media:00000, site:000000, spot:000000, type:1,取向:1}; 0< / script>
< script type = text / javascript src = https://js1.nend.net/js/nendAdLoader.js>< / script>
1。有一段时间我尝试了一个带有类似句子的'dart:js'
js.context.callMethod(..
但是,认为它不起作用,因为我们需要展示广告,不仅执行javascript代码。
2.next,我想它可以是WebView(Web应用程序上的Webview。),然后尝试使用下一个:
依赖项:
webview_flutter:^ 0.3.14
,但是它掉了
错误:PlatformException(未注册工厂,未为视图类型'plugins.flutter.io/webview'注册工厂。
也许是一个积极的经历者,或者可以给我任何建议。
如果您只需要在网页中使用这些脚本,则可以尝试将其直接包含在 web / Index.html
页面中。
否则,您将必须使用 ui.platformViewRegistry.registerViewFactory
此处
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.
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.
这篇关于是否可以-在Flutter Web应用程序中插入Google AdSense?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!