Search code examples
flutteradsenseflutter-web

How to insert Adsense display ad in flutter web


I created a fixed display ad 320px *60px. How do I insert in flutter web where I want? This is the code I obtained from adsense:

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:320px;height:60px"
     data-ad-client="xxxxxxxxx"
     data-ad-slot="xxxxxxxx"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

Or is it possible to make flutter not occupy the bottom 60 pixels of the screen and insert the adsense ad there by manipulating flt-glass-pane somehow?

I'm looking at a way to insert adsense ads to a mobile website built in flutter-web


Solution

  • I was able to insert ads into the listview in flutter-web as below: Created an html file adview.html

    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <ins class="adsbygoogle"
         style="display:inline-block;width:320px;height:80px"
         data-ad-client="ca-pub-xxxxxx"
         data-ad-slot="xxxxxxx"></ins>
    <script>
         (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    

    And then, an IFrameElement to make use of the html:

    // ignore: avoid_web_libraries_in_flutter
    import 'dart:html';
    import 'dart:ui' as ui;
    import 'package:flutter/material.dart';
    
    Widget adsenseAdsView() {
      // ignore: undefined_prefixed_name
      ui.platformViewRegistry.registerViewFactory(
          'adViewType',
          (int viewID) => IFrameElement()
            ..width = '320'
            ..height = '100'
            ..src = 'adview.html'
            ..style.border = 'none');
    
      return SizedBox(
        height: 100.0,
        width: 320.0,
        child: HtmlElementView(
          viewType: 'adViewType',
        ),
      );
    }
    

    Then we can add the widget from the function adsenseAdsView() wherever we want. I added it in ListView.