Menu

Banner ads

Importing the Banner Ad SDK:

swift Copy
import 'package:anythink_sdk/at_index.dart';

Use the following code to load a Banner ad

swift Copy
 _loadBannerAd() async {
         ATBannerManager
        .loadBannerAd(placementID: 'you 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 ratio.  
      ATBannerManager.createLoadbannerAdSize(width, width * (50 / 320)),
    });
  }

ATBannerManager.createLoadbannerAdSize(width,height) function description:

width: The width of the Banner display area

height: The height of the Banner display area

Note: This parameter must be passed when loading a Banner ad, otherwise the Banner may not achieve the expected display effect.

Admob adaptive banner:

swift Copy
// For the Admob platform, supports Admob banner ad adaptation

    //ATBannerManager.adaptiveOrientationCurrent() adaptive
    //ATBannerManager.adaptiveOrientationPortrait() portrait
    //ATBannerManager.adaptiveOrientationLandscape() landscape

 _loadBannerAd() async {
        ATBannerManager
        .loadBannerAd(placementID: 'you placementId', extraMap: {
      ATCommon.getAdSizeKey():
        ATBannerManager.createLoadbannerAdSize(width, width * (50 / 320)),
      ATBannerManager.getAdaptiveWidthKey(): width,
      ATBannerManager.getAdaptiveOrientationKey(): ATBannerManager.adaptiveOrientationCurrent(),
    });
  }

1.2 Check if There Is a Cached Ad and Get Ad Status

Use the following code to check if there is a cached ad:

swift Copy
_hasBannerAdReady() async {
    await ATBannerManager
        .bannerAdReady(
      placementID: 'b5bacad80a0fb1',
    )
        .then((value) {
      print('flutter Banner ad video cache $value');
    });
  }

Use the following code to get the ad status (return value type is Map). The key-value pairs are as follows:

  1. isLoading: Whether it is loading

  2. isReady: Whether there is a cached ad

  3. adInfo: The highest-priority cached ad info

swift Copy
_checkBannerAdStatus() async {
    await ATBannerManager
        .checkBannerLoadStatus(
      placementID: 'b5bacad80a0fb1',
    ).then((value) {
      print('flutter Banner ad video status $value');
    });
  }

Get information about all available ads under the current ad placement

swift Copy
getBannerValidAds() async {
    await ATBannerManager
        .getBannerValidAds(
      placementID: Configuration.bannerPlacementID,
    ).then((value) {
      print('flutter Banner ad video status $value');
    });
  }

1.3 Enter Scenario

swift Copy
entryBannerScenario() async {
        ATBannerManager.entryBannerScenario(
        placementID:  'you placementId',
        sceneID: 'you sceneID',
        );
  }

1.4.1 platformView

When the Banner ad implementation method is platformView, you only need to initialize the PlatformBannerWidget component after the ad loads successfully. Note that when using this method to display a Banner ad, the following methods cannot be used.

Note: It is recommended to properly manage the Banner view state to avoid blank Banner ads when Flutter frequently refreshes the interface.

swift Copy
// Hide Banner ad
ATBannerManager.hideBannerAd(placementID:'xxx')
// Remove Banner ad
ATBannerManager.removeBannerAd(placementID:'xxx')
// Display Banner ad
ATBannerManager.afreshShowBannerAd(placementID:'xxx')

PlatformBannerWidget property description:

placementID: Ad ID

sceneID: Scenario ID (optional parameter)

swift Copy
@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,
          ),
        )
      ),
    );
  }
}

1.4.2 Show API

This method renders a view and lays it out on the interface. We provide several APIs for use.

swift Copy
// Display using coordinates
ATBannerManager.showBannerInRectangle(placementID:'xx',extraMap:{});
// Display using coordinates and distinguish scenarios
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 a scenario ID, providing fixed bottom (getAdATBannerAdShowingPositionBottom) and fixed top (getAdATBannerAdShowingPositionTop)
ATBannerManager.showSceneBannerAdInPosition(placementID:'xx',sceneID:'xx',position:ATCommon.getAdATBannerAdShowingPositionBottom());

The view can be managed using the following methods:

swift Copy
// Hide Banner ad 
ATBannerManager.hideBannerAd(placementID:'xxx') 
// Remove Banner ad 
ATBannerManager.removeBannerAd(placementID:'xxx') 
// Display Banner ad 
ATBannerManager.afreshShowBannerAd(placementID:'xxx')

ATBannerResponse property description:

BannerStatus: Banner ad status

placementID: placementID

requestMessage: Request info (error message)

extraMap: Callback Info

isDeeplinkSuccess: isDeeplinkSuccess

To get notified about various Banner ad events (load success/failure, impression, and click), the sample code is as follows:

swift Copy
_bannerListen() {

    ATListenerManager.bannerEventHandler.listen((value) {

      switch (value.bannerStatus) {
        // Ad load failed
        case BannerStatus.bannerAdFailToLoadAD:
          print("flutter bannerAdFailToLoadAD ---- placementID: ${value.placementID} ---- errStr:${value.requestMessage}");
          break;
        // Ad loaded successfully  
        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  
        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 tapped  
        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;
      }
    });
  }
Previous
Interstitials ads
Next
Native ads
Last modified: 2026-07-01Powered by