3

我正在尝试实现一个非常基本的事情:在顶部显示 Admob 横幅广告。这可行,但横幅广告滑入状态栏,我找不到任何方法让它正确显示。这是我正在尝试的示例代码:

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: HomeView(),
    );
  }
}

class HomeView extends StatefulWidget {
  @override
  _HomeViewState createState() => _HomeViewState();
}

Future<void> _initAdMob() {
  return FirebaseAdMob.instance.initialize(appId: AdManager.appId);
}

class _HomeViewState extends State<HomeView> {
  // COMPLETE: Add _bannerAd
  BannerAd _bannerAd;

  // COMPLETE: Implement _loadBannerAd()
  void _loadBannerAd() {
    _bannerAd
      ..load()
      ..show(anchorType: AnchorType.top);
  }
  @override
  void initState() {
    _bannerAd = BannerAd(
      adUnitId: AdManager.bannerAdUnitId,
      size: AdSize.banner,
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<void>(
      future: _initAdMob(),
      builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
        _loadBannerAd();
        return SafeArea(
          child: new Column(children: <Widget>[
            Text('Sample text')
          ],),
        );
      },
    );
  }
}

此代码产生以下输出: Admob Banner 广告输出

这显然是错误的。文字就在它应该在的地方,但是,横幅广告滑入状态栏,这不是我想要的。另外,由于谷歌不支持为横幅广告提供位置参数,我完全无能为力。

但是,Google Codelab for Flutter中的 Banner 广告效果很好,这超出了我作为 Flutter 新手的理解。

有人可以阐明并指导我示例代码有什么问题吗?

4

0 回答 0