flutter_google_ad_manager 0.6.3+2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 86

flutter_google_ad_manager #

Flutter plugin for Google Ad Manager(DoubleClick for Publishers).

Getting Started #

ios #

Add io.flutter.embedded_views_preview in info.plist

<key>io.flutter.embedded_views_preview</key>
<true/>

Follow any additional instructions found here

Google Ad Manager Getting Started Guide

Android #

Add com.google.android.gms.ads.AD_MANAGER_APP in AndroidManifest.xml

<manifest>
    <application>
        <meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true"/>
    </application>
</manifest>

Follow any additional instructions found here

Google Ad Manager Getting Started Guide

Banner Ads #

Just write the DFPBanner widget in your favorite place.

DFPBanner(
  isDevelop: true,
  testDevices: MyTestDevices(),
  adUnitId: '/XXXXXXXXX/XXXXXXXXX',
  adSize: DFPAdSize.BANNER,
  onAdLoaded: () {
    print('Banner onAdLoaded');
  },
  onAdFailedToLoad: (errorCode) {
    print('Banner onAdFailedToLoad: errorCode:$errorCode');
  },
  onAdOpened: () {
    print('Banner onAdOpened');
  },
  onAdClosed: () {
    print('Banner onAdClosed');
  },
  onAdLeftApplication: () {
    print('Banner onAdLeftApplication');
  },
),

About testDevices #

If you set isDevelop to true, the test adUnitId will be used.
( If set false, the described adUnitId is used.)
Google Ad Manger needs to register testDevices in case of actual devices.

With this plug-in, you can create the following class and set it to testDevices of DFPBanner.

class MyTestDevices extends TestDevices {
  static MyTestDevices _instance;

  factory MyTestDevices() {
    if (_instance == null) _instance = new MyTestDevices._internal();
    return _instance;
  }

  MyTestDevices._internal();

  @override
  List<String> get values => List()..add("XXXXXXXX"); // Set here.
}
DFPBanner(
  testDevices: MyTestDevices(),

About adSize #


* BANNER
* FULL_BANNER
* LARGE_BANNER
* LEADERBOARD
* MEDIUM_RECTANGLE
* SMART_BANNER (Only Portrait)

Other custom is also available.

About EventListener #

Event listeners are also available.
However, ios does not work. (I am conducting an investigation, but I do not know yet.) If you really want to use it, you can use listener by bringing Plugin source to your application.

Interstitial Ads #

Firstly load it and call the show method at the desired timing.

DFPInterstitialAd _interstitialAd;

@override
void initState() {
  super.initState();

  _interstitialAd = DFPInterstitialAd(
    isDevelop: true,
    adUnitId: "XXXXXXXX",
    onAdLoaded: () {
      print('interstitialAd onAdLoaded');
    },
    onAdFailedToLoad: (errorCode) {
      print('interstitialAd onAdFailedToLoad: errorCode:$errorCode');
    },
    onAdOpened: () {
      print('interstitialAd onAdOpened');
    },
    onAdClosed: () {
      print('interstitialAd onAdClosed');
      interstitialAd.load();
    },
    onAdLeftApplication: () {
      print('interstitialAd onAdLeftApplication');
    },
  );
  _interstitialAd.load();
}

@override
void dispose() {
  _interstitialAd.dispose();
  super.dispose();
}
await interstitialAd.show();

About isDevelop #

If you set isDevelop to true, the test adUnitId will be used.
( If set false, the described adUnitId is used.)

About EventListener #

It is similar to the above Banner.
It does not work on ios.

Because of this, it can not be reloaded after closing, so it can not be displayed twice on the same screen.

Rewarded Ads #

firstly load it and call the show method at the desired timing.

DFPRewardedAd _rewardedAd;
LifecycleEventHandler _lifecycle;

@override
void initState() {
  super.initState();
  _rewardedAd = DFPRewardedAd(
    isDevelop: true,
    adUnitId: "XXXXXXX",
    onAdLoaded: () {
      print('rewardedAd onAdLoaded');
    },
    onAdFailedToLoad: (errorCode) {
      print('rewardedAd onAdFailedToLoad: errorCode:$errorCode');
    },
    onAdOpened: () {
      print('rewardedAd onAdOpened');
    },
    onAdClosed: () {
      print('rewardedAd onAdClosed');
      _rewardedAd.load();
    },
    onAdLeftApplication: () {
      print('rewardedAd onAdLeftApplication');
    },
    onRewarded: (String type, int amount) {
      print('rewardedAd onRewarded: type:$type amount:$amount');
    },
    onVideoStarted: () {
      print('rewardedAd onVideoStarted');
    },
    onVideoCompleted: () {
      print('rewardedAd onVideoCompleted');
    },
  );
  _rewardedAd.load();
  _lifecycle = LifecycleEventHandler(_rewardedAd);
  WidgetsBinding.instance.addObserver(_lifecycle);
}

@override
void dispose() {
  WidgetsBinding.instance.removeObserver(_lifecycle);
  _rewardedAd.dispose();
  super.dispose();
}
await _rewardedAd.show();

It is necessary to call it when resumed and paused, respectively. For that, please implement WidgetsBindingObserver.

class LifecycleEventHandler extends WidgetsBindingObserver {
  final DFPRewardedAd rewardedAd;

  LifecycleEventHandler(this.rewardedAd);

  @override
  didChangeAppLifecycleState(AppLifecycleState state) async {
    switch (state) {
      case AppLifecycleState.inactive:
        break;
      case AppLifecycleState.paused:
        await rewardedAd.pause();
        break;
      case AppLifecycleState.suspending:
        break;
      case AppLifecycleState.resumed:
        await rewardedAd.resume();
        break;
    }
  }
}

About isDevelop #

If you set isDevelop to true, the test adUnitId will be used.
( If set false, the described adUnitId is used.)

About EventListener #

Event listeners are also available.
This also works with ios and android.

Native Ads #

Not implemented.
I am glad if someone will give me a pull request.

0.6.3+2 #

  • Update a Document.

0.6.3 #

  • Fix to compilation error when Google-Mobile-Ads-SDK v7.42.2 or later on iOS.

0.6.2 #

  • Enhanced readme.
  • Modify example. (typo and so on.)

0.6.1 #

  • Bump compileSdkVersion
  • Fix to work in release mode
  • Adapted the version of play-services-ads to the other firebase plugin.

https://developers.google.com/android/guides/releases#november_13_2018

0.6.0 #

  • initial public release.

example/README.md

mport 'package:flutter/material.dart';
import 'package:flutter_google_ad_manager/flutter_google_ad_manager.dart';

void main() => runApp(MyApp());

class MyTestDevices extends TestDevices {
  static MyTestDevices _instance;

  factory MyTestDevices() {
    if (_instance == null) _instance = new MyTestDevices._internal();
    return _instance;
  }

  MyTestDevices._internal();

  @override
  List<String> get values => List()..add("XXXXXXXX");
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  DFPInterstitialAd _interstitialAd;
  DFPRewardedAd _rewardedAd;
  LifecycleEventHandler _lifecycle;

  @override
  void initState() {
    super.initState();

    _interstitialAd = DFPInterstitialAd(
      isDevelop: true,
      adUnitId: "XXXXXXXX",
      onAdLoaded: () {
        print('interstitialAd onAdLoaded');
      },
      onAdFailedToLoad: (errorCode) {
        print('interstitialAd onAdFailedToLoad: errorCode:$errorCode');
      },
      onAdOpened: () {
        print('interstitialAd onAdOpened');
      },
      onAdClosed: () {
        print('interstitialAd onAdClosed');
        _interstitialAd.load();
      },
      onAdLeftApplication: () {
        print('interstitialAd onAdLeftApplication');
      },
    );
    _interstitialAd.load();

    _rewardedAd = DFPRewardedAd(
      isDevelop: true,
      adUnitId: "XXXXXXX",
      onAdLoaded: () {
        print('rewardedAd onAdLoaded');
      },
      onAdFailedToLoad: (errorCode) {
        print('rewardedAd onAdFailedToLoad: errorCode:$errorCode');
      },
      onAdOpened: () {
        print('rewardedAd onAdOpened');
      },
      onAdClosed: () {
        print('rewardedAd onAdClosed');
        _rewardedAd.load();
      },
      onAdLeftApplication: () {
        print('rewardedAd onAdLeftApplication');
      },
      onRewarded: (String type, int amount) {
        print('rewardedAd onRewarded: type:$type amount:$amount');
      },
      onVideoStarted: () {
        print('rewardedAd onVideoStarted');
      },
      onVideoCompleted: () {
        print('rewardedAd onVideoCompleted');
      },
    );
    _rewardedAd.load();
    _lifecycle = LifecycleEventHandler(_rewardedAd);
    WidgetsBinding.instance.addObserver(_lifecycle);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(_lifecycle);
    _interstitialAd.dispose();
    _rewardedAd.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
        padding: EdgeInsets.all(20.0),
        itemCount: 10,
        itemBuilder: (BuildContext context, int index) {
          if (index != 0 && index % 4 == 0) {
            return Column(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(top: 16, bottom: 16.0),
                  child: DFPBanner(
                    isDevelop: true,
                    testDevices: MyTestDevices(),
                    adUnitId: 'XXXXXXXXX/XXXXXXXXX',
                    adSize: DFPAdSize.BANNER,
                    onAdLoaded: () {
                      print('Banner onAdLoaded');
                    },
                    onAdFailedToLoad: (errorCode) {
                      print('Banner onAdFailedToLoad: errorCode:$errorCode');
                    },
                    onAdOpened: () {
                      print('Banner onAdOpened');
                    },
                    onAdClosed: () {
                      print('Banner onAdClosed');
                    },
                    onAdLeftApplication: () {
                      print('Banner onAdLeftApplication');
                    },
                  ),
                ),
              ],
            );
          }
          return SizedBox(
            height: 100.0,
            child: Card(
              child: Center(
                child: Text('$index'),
              ),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await _rewardedAd.show();
          //await interstitialAd.show();
        },
        tooltip: '?',
        child: Icon(Icons.add),
      ),
    );
  }
}

class LifecycleEventHandler extends WidgetsBindingObserver {
  final DFPRewardedAd rewardedAd;

  LifecycleEventHandler(this.rewardedAd);

  @override
  didChangeAppLifecycleState(AppLifecycleState state) async {
    switch (state) {
      case AppLifecycleState.inactive:
        break;
      case AppLifecycleState.paused:
        await rewardedAd.pause();
        break;
      case AppLifecycleState.suspending:
        break;
      case AppLifecycleState.resumed:
        await rewardedAd.resume();
        break;
    }
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_google_ad_manager: ^0.6.3+2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_google_ad_manager/flutter_google_ad_manager.dart';
  
Version Uploaded Documentation Archive
0.6.3+2 Apr 15, 2019 Go to the documentation of flutter_google_ad_manager 0.6.3+2 Download flutter_google_ad_manager 0.6.3+2 archive
0.6.3+1 Apr 15, 2019 Go to the documentation of flutter_google_ad_manager 0.6.3+1 Download flutter_google_ad_manager 0.6.3+1 archive
0.6.3 Apr 15, 2019 Go to the documentation of flutter_google_ad_manager 0.6.3 Download flutter_google_ad_manager 0.6.3 archive
0.6.2 Feb 18, 2019 Go to the documentation of flutter_google_ad_manager 0.6.2 Download flutter_google_ad_manager 0.6.2 archive
0.6.1 Feb 4, 2019 Go to the documentation of flutter_google_ad_manager 0.6.1 Download flutter_google_ad_manager 0.6.1 archive
0.6.0 Feb 2, 2019 Go to the documentation of flutter_google_ad_manager 0.6.0 Download flutter_google_ad_manager 0.6.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
72
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

We analyzed this package on Apr 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14
  • Flutter: 1.4.7

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/ad_size.dart.

Run flutter format to format lib/ad_size.dart.

Format lib/banner.dart.

Run flutter format to format lib/banner.dart.

Format lib/interstitial_ad.dart.

Run flutter format to format lib/interstitial_ad.dart.

Format lib/rewarded_ad.dart.

Run flutter format to format lib/rewarded_ad.dart.

Format lib/test_devices.dart.

Run flutter format to format lib/test_devices.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8