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

前端 未结 1 1602
故里飘歌
故里飘歌 2020-12-29 00:48

I try to insert the next code(Google AdSense) at my site, but unsuccessful by now. The code is:

            

        
相关标签:
1条回答
  • 2020-12-29 01:14

    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.

    0 讨论(0)
提交回复
热议问题