首页 > 解决方案 > 如何在 Flutter Web 中插入 Adsense 展示广告

问题描述

我制作了一个 320px *60px 的固定展示广告。如何在我想要的地方插入颤振网页?这是我从 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>

或者是否可以通过某种方式操纵 flt-glass-pane 使颤动不占据屏幕底部 60 像素并在那里插入 adsense 广告?

我正在寻找一种将 adsense 广告插入内置于 flutter-web 的移动网站的方法

标签: flutteradsenseflutter-web

解决方案


我能够将广告插入到 flutter-web 中的列表视图中,如下所示: 创建了一个 html 文件 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>

然后,使用 html 的 IFrameElement:

// 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',
    ),
  );
}

然后我们可以从函数 adsenseAdsView() 中将小部件添加到我们想要的任何地方。我在 ListView 中添加了它。


推荐阅读