flutter google ad manager
flutter plugin for google ad manager (doubleclick for publishers).
getting started with google ad manager
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');
},
),
manual ad refresh
you can manually refresh the banner ads on user interaction, use the onadviewcreated
callback to save an instance of the dfpbannerviewcontroller and then call the reload() method to refresh the ad.
dfpbannerviewcontroller _bannerviewcontroller;
_reload() {
_bannerviewcontroller?.reload();
}
dfpbanner(
isdevelop: true,
testdevices: mytestdevices(),
adunitid: '/xxxxxxxxx/xxxxxxxxx',
adsize: dfpadsize.banner,
onadviewcreated: (controller) {
_bannerviewcontroller = controller;
},
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
dfpadsize
is available. this is the same size as that of android.
- banner
- full_banner
- large_banner
- leaderboard
- medium_rectangle
- smart_banner (only portrait)
other custom is also available.
const dfpadsize.custom({double width, double height})
.
about eventlistener
the following event listeners are available:
- onadloaded
- onadfailedtoload
- onadopened
- onadclosed
- onadleftapplication
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
the following event listeners are available:
- onadloaded
- onadfailedtoload
- onadopened
- onadclosed
- onadleftapplication
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
the following event listeners are available:
- onadloaded
- onadfailedtoload
- onadopened
- onadclosed
- onadleftapplication
- onrewarded
- onvideostarted
- onvideocompleted
native ads
not implemented.
i am glad if someone will give me a pull request.
Comments are closed.