This repository has been archived by the owner on Mar 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 58
Using the controller and listening to native events
Bruno D'Luka edited this page Mar 6, 2021
·
9 revisions
To have better control over the ad, you can use a NativeAdController
:
// Init the controller
final controller = NativeAdController();
// Use the controller
@override
Widget build(BuildContext context {
return NativeAd(controller: controller);
}
// Dispose the controller to free up resources.
// You can't use the it again once it's disposed
@override
void dispose() {
controller.dispose();
super.dispose();
}
To load
or reload
the ad, use controller.load()
You can optionally use NativeAdOptions
and set another unitId
that isn't the default one:
controller.load(
options: NativeAdOptions(...),
unitId: yourUnitId,
loadTimeout: Duration(minutes: 1),
);
You can check if the ad is loading by calling controller.isLoaded
You can listen to events to have a better control over an ad. You can listen to events using:
controller.onEvent.listen
controller.onVideoEvent.listen
Common events are events that aren't thrown by a video. Avaiable events:
- loading
- loaded
- loadFailed
- muted
- undefined
@override
void initState() {
super.initState();
controller.onEvent.listen((e) {
final event = e.keys.first;
switch (event) {
case NativeAdEvent.loading:
print('loading');
break;
case NativeAdEvent.loaded:
print('loaded');
break;
case NativeAdEvent.loadFailed:
final errorCode = e.values.first;
print('loadFailed $errorCode');
break;
case NativeAdEvent.muted:
showDialog(
...,
builder: (_) => AlertDialog(title: Text('Ad muted')),
);
break;
default:
break;
}
});
}
Video events thrown by a Native Video Ad. The fastest way to get a native video ad is using the video ad test unit id: MobileAds.nativeVideoAdTestUnitId
. Avaiable events:
- start
- play
- pause
- end
- muted
- unmuted
@override
void initState() {
super.initState();
controller.onVideoEvent.listen((e) {
final event = e.keys.first;
switch (event) {
case AdVideoEvent.start:
print('video started');
break;
case NativeAdEvent.play:
print('video played');
break;
case AdVideoEvent.pause:
print('video paused');
break;
case AdVideoEvent.end:
print('video finished');
break;
case AdVideoEvent.muted;
print('video muted');
break;
case AdVideoEvent.unmuted;
print('video unmuted');
break;
default:
break;
}
});
}
Next: Ad Options |
---|