# InterstitialAd

Displays a full-screen interstitial ad.

Overview

Interstitial ads are full-screen ads that can be displayed at natural transition points in the flow of an application, such as between activities or during the pause between game levels.

Platform Support

  • **Supported:** Mobile platforms (Android, iOS).
  • **Unsupported:** Web, Desktop.
  • **Exception:** Raises FletUnsupportedPlatformException when used on unsupported platforms.
  • Inheritance

    This control inherits properties and methods from BaseAd.

    Methods

    show()

  • **Signature:** async show()
  • **Description:** Asynchronously displays the interstitial ad. This method should be called when the ad is ready to be shown.
  • Events

  • on_load: Triggered when the ad has successfully loaded.
  • on_error: Triggered when an error occurs during ad loading or display. Receives an error data payload.
  • on_open: Triggered when the ad is opened (e.g., user clicks on it).
  • on_close: Triggered when the ad is closed by the user.
  • on_impression: Triggered when an ad impression is recorded.
  • on_click: Triggered when the ad is clicked by the user.
  • Example Usage

    python import flet_ads as fta import flet as ft

    def main(page: ft.Page): page.horizontal_alignment = ft.CrossAxisAlignment.CENTER page.appbar = ft.AppBar( adaptive=True, title="Mobile Ads Playground", bgcolor=ft.Colors.LIGHT_BLUE_300, )

    # Test ad unit IDs (replace with your actual IDs) ids = { ft.PagePlatform.ANDROID: { "banner": "ca-app-pub-3940256099942544/6300978111", "interstitial": "ca-app-pub-3940256099942544/1033173712", }, ft.PagePlatform.IOS: { "banner": "ca-app-pub-3940256099942544/2934735716", "interstitial": "ca-app-pub-3940256099942544/4411468910", }, }

    def handle_interstitial_ad_close(e: ft.Event[fta.InterstitialAd]): nonlocal iad print("Closing InterstitialAd") page.overlay.remove(e.control) page.overlay.append(iad := get_new_interstitial_ad()) page.update()

    async def handle_interstitial_ad_display(e: ft.Event[ft.OutlinedButton]): nonlocal iad print("Showing InterstitialAd") await iad.show()

    def get_new_interstitial_ad(): return fta.InterstitialAd( unit_id=ids.get(page.platform, {}).get("interstitial"), on_load=lambda e: print("InterstitialAd loaded"), on_error=lambda e: print("InterstitialAd error", e.data), on_open=lambda e: print("InterstitialAd opened"), on_close=handle_interstitial_ad_close, on_impression=lambda e: print("InterstitialAd impression"), on_click=lambda e: print("InterstitialAd clicked"), )

    page.overlay.append(iad := get_new_interstitial_ad())

    page.add( ft.OutlinedButton( content="Show InterstitialAd", on_click=handle_interstitial_ad_display, ) )

    ft.run(main)

    
    

    Properties (Inherited from BaseAd)

  • unit_id: String. The ad unit ID for the ad. Required for displaying ads.
  • on_load: Event handler for ad load success.
  • on_error: Event handler for ad load errors.
  • on_open: Event handler for ad open.
  • on_close: Event handler for ad close.
  • on_impression: Event handler for ad impression.
  • on_click: Event handler for ad click.
  • Constraints

  • Must be initialized with a valid unit_id for the target platform.
  • show() must be called on a loaded ad.