is it possible - to insert google AdSense at Flutter Web application?

北战南征 提交于 2020-06-22 13:10:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!