Import Banner Ad SDK:
import 'package:secmtp_sdk/at_index.dart';
import 'package:thinkup_sdk/at_index.dart';
Use the following code to load Banner ad
_loadBannerAd() async {
await ATBannerManager
.loadBannerAd(placementID: 'your placementId', extraMap: {
ATCommon.getAdSizeKey():
// The height is calculated based on the banner aspect ratio of 320:50. For other aspect ratios, calculate according to the actual situation.
ATBannerManager.createLoadbannerAdSize(width, width * (50 / 320)),
});
}
Description of ATBannerManager.createLoadbannerAdSize(width,height) function:
width: Width of the Banner display area
height: Height of the Banner display area
Note: This parameter must be passed when loading a Banner ad; otherwise, the Banner may not display as expected.
Admob Adaptive Banner:
// For the Admob platform, supports Admob banner ad adaptation
//ATBannerManager.adaptiveOrientationCurrent() - Adaptive
//ATBannerManager.adaptiveOrientationPortrait() - Portrait
//ATBannerManager.adaptiveOrientationLandscape() - Landscape
_loadBannerAd() async {
await ATBannerManager
.loadBannerAd(placementID: 'your placementId', extraMap: {
ATCommon.getAdSizeKey():
ATBannerManager.createLoadbannerAdSize(width, width * (50 / 320)),
ATBannerManager.getAdaptiveWidthKey(): width,
ATBannerManager.getAdaptiveOrientationKey(): ATBannerManager.adaptiveOrientationCurrent(),
});
}
Use the following code to check if there is ad cache:
_hasBannerAdReady() async {
await ATBannerManager
.bannerAdReady(
placementID: 'b5bacad80a0fb1',
)
.then((value) {
print('flutter banner ad cache: $value');
});
}
entryBannerScenario() async {
await ATBannerManager.entryBannerScenario(
placementID: 'your placementId',
sceneID: 'your sceneID',
);
}
When the Banner ad is implemented via PlatformView, you only need to initialize the PlatformBannerWidget component after the ad is loaded successfully to use it. Note that the following methods cannot be used for Banner ads displayed in this way.
Note: It is recommended to properly control the Banner view state to avoid blank display of the Banner ad when the Flutter interface is frequently refreshed.
// Hide banner ad
ATBannerManager.hideBannerAd(placementID:'xxx')
// Remove banner ad
ATBannerManager.removeBannerAd(placementID:'xxx')
// Show banner ad again
ATBannerManager.afreshShowBannerAd(placementID:'xxx')
Description of PlatformBannerWidget properties:
placementID: Ad ID
sceneID: Scene ID (optional parameter)
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("native text"),
),
body: SafeArea(
child: Container(
height: 200,
color: Colors.black,
child: PlatformBannerWidget(
Configuration.bannerPlacementID,
),
)
),
);
}
}
This method renders the view and layouts it on the interface. We provide several APIs for use:
// Display using coordinates
ATBannerManager.showBannerInRectangle(placementID:'xx',extraMap:{});
// Display using coordinates and distinguish scenes
ATBannerManager.showSceneBannerInRectangle(placementID:'xx',sceneID:'xxx',extraMap:{})
// Display at a fixed position, providing fixed bottom (getAdATBannerAdShowingPositionBottom) and fixed top (getAdATBannerAdShowingPositionTop)
ATBannerManager.showAdInPosition(placementID:'xx',position:ATCommon.getAdATBannerAdShowingPositionBottom());
// Display at a fixed position with scene ID, providing fixed bottom (getAdATBannerAdShowingPositionBottom) and fixed top (getAdATBannerAdShowingPositionTop)
ATBannerManager.showSceneBannerAdInPosition(placementID:'xx',sceneID:'xx',position:ATCommon.getAdATBannerAdShowingPositionBottom());
You can manage the view in the following ways:
// Hide banner ad
ATBannerManager.hideBannerAd(placementID:'xxx')
// Remove banner ad
ATBannerManager.removeBannerAd(placementID:'xxx')
// Show banner ad again
ATBannerManager.afreshShowBannerAd(placementID:'xxx')
Introduction to ATBannerResponse Properties:
BannerStatus: Banner ad status
placementID: Ad placement ID
requestMessage: Request information (error information)
extraMap: Callback Information
isDeeplinkSuccess: Whether DeepLink is successful
To receive notifications about various Banner ad events (load success/failure, display, and click), the sample code is as follows:
_bannerListen() {
ATListenerManager.bannerEventHandler.listen((value) {
switch (value.bannerStatus) {
// Ad loading failed
case BannerStatus.bannerAdFailToLoadAD:
print("flutter bannerAdFailToLoadAD ---- placementID: ${value.placementID} ---- errStr:${value.requestMessage}");
break;
// Ad loading succeeded
case BannerStatus.bannerAdDidFinishLoading:
print("flutter bannerAdDidFinishLoading ---- placementID: ${value.placementID}");
break;
// Ad auto-refresh succeeded
case BannerStatus.bannerAdAutoRefreshSucceed:
print("flutter bannerAdAutoRefreshSucceed ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
break;
// Ad clicked
case BannerStatus.bannerAdDidClick:
print("flutter bannerAdDidClick ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
break;
// DeepLink triggered
case BannerStatus.bannerAdDidDeepLink:
print("flutter bannerAdDidDeepLink ---- placementID: ${value.placementID} ---- extra:${value.extraMap} ---- isDeeplinkSuccess:${value.isDeeplinkSuccess}");
break;
// Ad displayed successfully
case BannerStatus.bannerAdDidShowSucceed:
print("flutter bannerAdDidShowSucceed ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
break;
// Ad close button clicked
case BannerStatus.bannerAdTapCloseButton:
print("flutter bannerAdTapCloseButton ---- placementID: ${value.placementID} ---- extra:${value.extraMap}");
break;
// Ad auto-refresh failed
case BannerStatus.bannerAdAutoRefreshFail:
print("flutter bannerAdAutoRefreshFail ---- placementID: ${value.placementID} ---- errStr:${value.requestMessage}");
break;
case BannerStatus.bannerAdUnknown:
print("flutter bannerAdUnknown");
break;
}
});
}