简体   繁体   English

flutter_native_admob 预加载广告

[英]flutter_native_admob preload ads

is it possible to preload the native flutter admob ads, for example in initState?是否可以预加载原生 flutter admob 广告,例如在 initState 中?

I am trying to use the ads in a ListView.builder() filled with user-related content and the ads in between.我正在尝试使用ListView.builder()的广告,其中填充了与用户相关的内容以及介于两者之间的广告。 But the ads are loaded too late so that the user can see the spinner appear on screen if he scrolls through the ListView .但是广告加载得太晚了,因此用户在滚动ListView可以看到屏幕上出现微调器。

Example code (from https://pub.dev/packages/flutter_native_admob ):示例代码(来自https://pub.dev/packages/flutter_native_admob ):

@override
Widget build(BuildContext context) {
return MaterialApp(
  home: Scaffold(
    appBar: AppBar(
      title: const Text('Plugin example app'),
    ),
    body: ListView(
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(bottom: 20.0),
          height: 200.0,
          color: Colors.green,
        ),
        Container(
          margin: EdgeInsets.only(bottom: 20.0),
          height: 200.0,
          color: Colors.green,
        ),
        Container(
          margin: EdgeInsets.only(bottom: 20.0),
          height: 200.0,
          color: Colors.green,
        ),
        NativeAdmobBannerView(                // <---- preload this ad
          adUnitID: "<Your ad unit ID>",
          style: BannerStyle.dark, // enum dark or light
          showMedia: true, // whether to show media view or not
          contentPadding: EdgeInsets.all(10), // content padding
          onCreate: (controller) {
            controller.setStyle(BannerStyle.light); // Dynamic update style
          },
        ),
        Container(
          margin: EdgeInsets.only(bottom: 20.0),
          height: 200.0,
          color: Colors.green,
        ),
        Container(
          margin: EdgeInsets.only(bottom: 20.0),
          height: 200.0,
          color: Colors.green,
        ),
        Container(
          margin: EdgeInsets.only(bottom: 20.0),
          height: 200.0,
          color: Colors.green,
        ),
      ],
    ),
  ),
);
}

Thanks in advance.提前致谢。

the creator of the flutter_native_admob plugin did update the plugin. flutter_native_admob 插件的创建者确实更新了插件。 You can now wait for the ad to be loaded and render it afterwards.您现在可以等待广告加载并在之后呈现它。 For further info, check the pub-page: https://pub.dev/packages/flutter_native_admob有关更多信息,请查看发布页面: https : //pub.dev/packages/flutter_native_admob

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_native_admob/flutter_native_admob.dart';
import 'package:flutter_native_admob/native_admob_controller.dart';

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static const _adUnitID = "<Your ad unit ID>";

  final _nativeAdController = NativeAdmobController();
  double _height = 0;

  StreamSubscription _subscription;

  @override
  void initState() {
    _subscription = _nativeAdController.stateChanged.listen(_onStateChanged);
    super.initState();
  }

  @override
  void dispose() {
    _subscription.cancel();
    _nativeAdController.dispose();
    super.dispose();
  }

  void _onStateChanged(AdLoadState state) {
    switch (state) {
      case AdLoadState.loading:
        setState(() {
          _height = 0;
        });
        break;

      case AdLoadState.loadCompleted:
        setState(() {
          _height = 330;
        });
        break;

      default:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: ListView(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(bottom: 20.0),
              height: 200.0,
              color: Colors.green,
            ),
            Container(
              margin: EdgeInsets.only(bottom: 20.0),
              height: 200.0,
              color: Colors.green,
            ),
            Container(
              margin: EdgeInsets.only(bottom: 20.0),
              height: 200.0,
              color: Colors.green,
            ),
            Container(
              height: _height,
              padding: EdgeInsets.all(10),
              margin: EdgeInsets.only(bottom: 20.0),
              child: NativeAdmob(
                // Your ad unit id
                adUnitID: _adUnitID,
                controller: _nativeAdController,

                // Don't show loading widget when in loading state
                loading: Container(),
              ),
            ),
            Container(
              margin: EdgeInsets.only(bottom: 20.0),
              height: 200.0,
              color: Colors.green,
            ),
            Container(
              margin: EdgeInsets.only(bottom: 20.0),
              height: 200.0,
              color: Colors.green,
            ),
            Container(
              margin: EdgeInsets.only(bottom: 20.0),
              height: 200.0,
              color: Colors.green,
            ),
          ],
        ),
      ),
    );
  }
}

I fixed this issue using some trick.我使用一些技巧解决了这个问题。 There may be more efficient way, but my solution works properly for me.可能有更有效的方法,但我的解决方案适合我。 What I did is: I create a NativeAdmob() widget with the opacity 0 .我所做的是:我创建了一个不透明度为0NativeAdmob()小部件。 Once the app is loaded, this widget behaves like preloaded widget which never shows on screen.加载应用程序后,此小部件的行为类似于从不显示在屏幕上的preloaded widget

Opacity(opacity: 0, child: NativeAdmob(
                    // Your ad unit id
                    adUnitID: _adUnitID,
                    controller: _nativeAdController,
                    loading: Container(),
                  ),
                ),

Then once you trigger another NativeAdmob() widget in the same page, the preloaded ads will be displayed without any loading process.然后,一旦您在同一页面中触发另一个NativeAdmob()小部件,则无需任何加载过程即可显示preloaded ads After showing the first ads, you can refresh it whenever you want using the code line below:展示第一个广告后,您可以随时使用以下代码行刷新它:

_nativeAdController.reloadAd(forceRefresh: true, numberAds: 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM