flet icon indicating copy to clipboard operation
flet copied to clipboard

Mobile Ads

Open ndonkoHenri opened this issue 1 year ago • 1 comments

Closes #286

Test Code:

import flet as ft
import flet.ads as ads


def main(page: ft.Page):
    page.overlay.append(
        iad := ads.InterstitialAd(
            unit_id="ca-app-pub-3940256099942544/1033173712",
            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=lambda e: print("InterstitialAd closed"),
            on_impression=lambda e: print("InterstitialAd impression"),
            on_click=lambda e: print("InterstitialAd clicked"),
        )
    )
    page.update()

    page.add(
        ft.SafeArea(
            content=ft.Column(
                controls=[
                    ft.Container(
                        content=ads.BannerAd(
                            unit_id="ca-app-pub-3940256099942544/6300978111",
                            on_click=lambda e: print("BannerAd clicked"),
                            on_load=lambda e: print("BannerAd loaded"),
                            on_error=lambda e: print("BannerAd error", e.data),
                            on_open=lambda e: print("BannerAd opened"),
                            on_close=lambda e: print("BannerAd closed"),
                            on_impression=lambda e: print("BannerAd impression"),
                            on_will_dismiss=lambda e: print("BannerAd will dismiss"),
                        ),
                        width=320,
                        height=50,
                        bgcolor="red",
                    ),
                    ft.OutlinedButton("show interstitial", on_click=lambda e: iad.show()),
                ]
            )
        )
    )


ft.app(main)

ndonkoHenri avatar May 14 '24 16:05 ndonkoHenri

Can you add (Rewarded interstitial - Interstitial Video - App Open)

zox47 avatar Jun 18 '24 10:06 zox47

Reviewer's Guide by Sourcery

This pull request introduces support for mobile ads (Banner and Interstitial) in the application. It includes the creation of new classes for handling different types of ads, integration of the flet_mobile_ads package into the main client application, and implementation of controls for handling these ads in Flutter. The changes ensure that the ads are properly initialized and managed within the application.

File-Level Changes

Files Changes
sdk/python/packages/flet-core/src/flet_core/ads/native.py
sdk/python/packages/flet-core/src/flet_core/ads/base_ad.py
sdk/python/packages/flet-core/src/flet_core/ads/interstitial.py
sdk/python/packages/flet-core/src/flet_core/ads/banner.py
Added new classes for handling different types of ads (Native, Base, Interstitial, Banner) with properties and event handlers.
packages/flet_mobile_ads/lib/src/native.dart
packages/flet_mobile_ads/lib/src/banner.dart
packages/flet_mobile_ads/lib/src/interstitial.dart
packages/flet_mobile_ads/lib/src/create_control.dart
Implemented controls for handling native, banner, and interstitial ads in Flutter, and created a control factory.
client/lib/main.dart
client/pubspec.yaml
Integrated flet_mobile_ads package into the main client application, including initialization and control creation.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

sourcery-ai[bot] avatar Jul 26 '24 14:07 sourcery-ai[bot]

Waiting on https://github.com/flet-dev/flet/issues/3345 to finalize (the unit ID for the ads is passed as property, but however needs to be added to iOS .plist and Android .xml equally)

ndonkoHenri avatar Jul 30 '24 08:07 ndonkoHenri

.

Waiting on #3345 to finalize (the unit ID for the ads is passed as property, but however needs to be added to iOS .plist and Android .xml equally)

please can you add native ads in documentation?

zox47 avatar Aug 17 '24 19:08 zox47

I faced some issues while trying to add Native ads (forgot which one though). It's files are present in this PR, but the Control can't be accessed nor used as its not fully ready-for-use.

For a start, we will have just Banner and Interstitial ads. Native might come up later on...

ndonkoHenri avatar Aug 18 '24 00:08 ndonkoHenri

@sourcery-ai review

ndonkoHenri avatar Nov 26 '24 21:11 ndonkoHenri

i test baner you ad code . and why error in app

"unknow control : banner_ad "

i test in android

version = 0.25.1

import flet as ft
import flet.ads as ads


def main(page: ft.Page):
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    

    id_banner = (
        "ca-app-pub-3940256099942544/6300978111"
        if page.platform == ft.PagePlatform.ANDROID
        else "ca-app-pub-3940256099942544/2934735716"
    )

   

   

    def display_new_banner_ad():
        page.add(
            ft.Container(
                content=ads.BannerAd(
                    unit_id=id_banner,
                    on_click=lambda e: print("BannerAd clicked"),
                    on_load=lambda e: print("BannerAd loaded"),
                    on_error=lambda e: print("BannerAd error", e.data),
                    on_open=lambda e: print("BannerAd opened"),
                    on_close=lambda e: print("BannerAd closed"),
                    on_impression=lambda e: print("BannerAd impression"),
                    on_will_dismiss=lambda e: print("BannerAd will dismiss"),
                ),
                width=320,
                height=50,
                bgcolor=ft.colors.TRANSPARENT,
            )
        )

    page.appbar = ft.AppBar(
        adaptive=True,
        title=ft.Text("Mobile Ads Playground"),
        bgcolor=ft.colors.LIGHT_BLUE_300,
    )
    page.add(
        ft.OutlinedButton("Show BannerAd", on_click=lambda e: display_new_banner_ad()),
    )


ft.app(main)

joesandiroz2 avatar Nov 30 '24 03:11 joesandiroz2

Using the Flet mobile app? Or you packaged as apk?

ndonkoHenri avatar Nov 30 '24 11:11 ndonkoHenri

Using the Flet mobile app? Or you packaged as apk?

Flet mobile . Yeah it shouldn't work . But is there a way to debug directly into android . Like . Flutter run android . That would be useful. When the development

joesandiroz2 avatar Nov 30 '24 11:11 joesandiroz2

But is there a way to debug directly into android . Like . Flutter run android . That would be useful. When the development

Taking note.

ndonkoHenri avatar Nov 30 '24 16:11 ndonkoHenri