flet-ads 0.0.1__py3-none-any.whl → 0.70.0.dev6644__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ import 'package:flet/flet.dart';
2
+ import 'package:flutter/widgets.dart';
3
+ import 'package:google_mobile_ads/google_mobile_ads.dart';
4
+
5
+ import '../utils/ads.dart';
6
+
7
+ class BannerAdControl extends StatefulWidget {
8
+ final Control control;
9
+
10
+ const BannerAdControl({super.key, required this.control});
11
+
12
+ @override
13
+ State<BannerAdControl> createState() => _BannerAdControlState();
14
+ }
15
+
16
+ class _BannerAdControlState extends State<BannerAdControl> with FletStoreMixin {
17
+ bool _isLoaded = false;
18
+
19
+ @override
20
+ Widget build(BuildContext context) {
21
+ debugPrint(
22
+ "BannerAd build: ${widget.control.id} (${widget.control.hashCode})");
23
+ final testAdUnitId = isIOSMobile()
24
+ ? 'ca-app-pub-3940256099942544/4411468910'
25
+ : 'ca-app-pub-3940256099942544/1033173712';
26
+ BannerAd bannerAd = BannerAd(
27
+ adUnitId: widget.control.getString("unit_id", testAdUnitId)!,
28
+ request:
29
+ parseAdRequest(widget.control.get("request"), const AdRequest())!,
30
+ size: AdSize.banner,
31
+ listener: BannerAdListener(
32
+ // Called when an ad is successfully received.
33
+ onAdLoaded: (ad) {
34
+ widget.control.triggerEvent("load");
35
+ setState(() {
36
+ _isLoaded = true;
37
+ });
38
+ },
39
+ // Called when an ad request failed.
40
+ onAdFailedToLoad: (ad, error) {
41
+ widget.control.triggerEvent("error", error.toString());
42
+ // Dispose the ad to free resources.
43
+ ad.dispose();
44
+ setState(() {
45
+ _isLoaded = false;
46
+ });
47
+ },
48
+ // Called when an ad opens an overlay that covers the screen.
49
+ onAdOpened: (Ad ad) {
50
+ widget.control.triggerEvent("open");
51
+ },
52
+ // Called when an ad removes an overlay that covers the screen.
53
+ onAdClosed: (Ad ad) {
54
+ widget.control.triggerEvent("close");
55
+ },
56
+ onAdClicked: (Ad ad) {
57
+ widget.control.triggerEvent("click");
58
+ },
59
+ onAdWillDismissScreen: (Ad ad) {
60
+ widget.control.triggerEvent("will_dismiss");
61
+ },
62
+ onPaidEvent: (ad, double valueMicros, PrecisionType precision,
63
+ String currencyCode) {
64
+ widget.control.triggerEvent("paid", {
65
+ "value": valueMicros,
66
+ "precision": precision.name,
67
+ "currency_code": currencyCode
68
+ });
69
+ },
70
+ // Called when an impression occurs on the ad.
71
+ onAdImpression: (Ad ad) {
72
+ widget.control.triggerEvent("impression");
73
+ },
74
+ ),
75
+ );
76
+
77
+ if (!_isLoaded) {
78
+ bannerAd.load();
79
+ }
80
+
81
+ return ConstrainedControl(
82
+ control: widget.control, child: AdWidget(ad: bannerAd));
83
+ }
84
+ }
@@ -0,0 +1,39 @@
1
+ import 'package:flet/flet.dart';
2
+ import 'package:flutter/cupertino.dart';
3
+ import 'package:google_mobile_ads/google_mobile_ads.dart';
4
+
5
+ import 'banner.dart';
6
+ import 'interstitial.dart';
7
+
8
+ class Extension extends FletExtension {
9
+ @override
10
+ void ensureInitialized() {
11
+ if (isMobilePlatform()) {
12
+ MobileAds.instance.initialize();
13
+ }
14
+ }
15
+
16
+ @override
17
+ FletService? createService(Control control) {
18
+ switch (control.type) {
19
+ case "InterstitialAd":
20
+ return InterstitialAdService(control: control);
21
+ default:
22
+ return null;
23
+ }
24
+ }
25
+
26
+ @override
27
+ Widget? createWidget(Key? key, Control control) {
28
+ switch (control.type) {
29
+ case "BannerAd":
30
+ return BannerAdControl(control: control);
31
+ /* TODO: Finalize NativeAdControl -> https://developers.google.com/admob/flutter/native/platforms
32
+ case "NativeAd":
33
+ return NativeAdControl(control: control);
34
+ */
35
+ default:
36
+ return null;
37
+ }
38
+ }
39
+ }
@@ -0,0 +1,70 @@
1
+ import 'package:flet/flet.dart';
2
+ import 'package:flutter/widgets.dart';
3
+ import 'package:google_mobile_ads/google_mobile_ads.dart';
4
+
5
+ import '../utils/ads.dart';
6
+
7
+ class InterstitialAdService extends FletService {
8
+ InterstitialAdService({required super.control});
9
+
10
+ static InterstitialAd? _interstitialAd;
11
+
12
+ @override
13
+ void init() {
14
+ super.init();
15
+ debugPrint("InterstitialAd(${control.id}).init: ${control.properties}");
16
+ control.addInvokeMethodListener(_invokeMethod);
17
+
18
+ InterstitialAd.load(
19
+ adUnitId: control.getString(
20
+ "unit_id",
21
+ isIOSMobile()
22
+ ? 'ca-app-pub-3940256099942544/4411468910'
23
+ : 'ca-app-pub-3940256099942544/1033173712')!,
24
+ request: parseAdRequest(control.get("request"), const AdRequest())!,
25
+ adLoadCallback: InterstitialAdLoadCallback(
26
+ onAdLoaded: (ad) {
27
+ ad.fullScreenContentCallback = FullScreenContentCallback(
28
+ onAdShowedFullScreenContent: (ad) => control.triggerEvent("open"),
29
+ onAdImpression: (ad) => control.triggerEvent("impression"),
30
+ onAdFailedToShowFullScreenContent: (ad, error) {
31
+ control.triggerEvent("error", error.toString());
32
+ ad.dispose(); // free resources
33
+ },
34
+ onAdDismissedFullScreenContent: (ad) {
35
+ // Called when the ad dismissed full screen content.
36
+ control.triggerEvent("close");
37
+ // Dispose the ad here to free resources.
38
+ ad.dispose();
39
+ },
40
+ onAdClicked: (ad) => control.triggerEvent("click"),
41
+ );
42
+
43
+ // Keep a reference to show it later.
44
+ _interstitialAd = ad;
45
+ control.triggerEvent("load");
46
+ },
47
+ onAdFailedToLoad: (LoadAdError error) {
48
+ control.triggerEvent("error", error.toString());
49
+ _interstitialAd?.dispose();
50
+ },
51
+ ));
52
+ }
53
+
54
+ Future<dynamic> _invokeMethod(String name, dynamic args) async {
55
+ debugPrint("InterstitialAd.$name($args)");
56
+ switch (name) {
57
+ case "show":
58
+ _interstitialAd?.show();
59
+ return null;
60
+ default:
61
+ throw Exception("Unknown InterstitialAd method: $name");
62
+ }
63
+ }
64
+
65
+ @override
66
+ void dispose() {
67
+ _interstitialAd?.dispose();
68
+ super.dispose();
69
+ }
70
+ }
@@ -0,0 +1,73 @@
1
+ import 'package:flet/flet.dart';
2
+ import 'package:flutter/material.dart';
3
+ import 'package:google_mobile_ads/google_mobile_ads.dart';
4
+
5
+ import '../utils/ads.dart';
6
+ import '../utils/native.dart';
7
+
8
+ class NativeAdControl extends StatefulWidget {
9
+ final Control control;
10
+
11
+ const NativeAdControl({super.key, required this.control});
12
+
13
+ @override
14
+ State<NativeAdControl> createState() => _NativeAdControlState();
15
+ }
16
+
17
+ class _NativeAdControlState extends State<NativeAdControl> with FletStoreMixin {
18
+ bool _isLoaded = false;
19
+
20
+ @override
21
+ Widget build(BuildContext context) {
22
+ debugPrint(
23
+ "NativeAd build: ${widget.control.id} (${widget.control.hashCode})");
24
+ final testAdUnitId = isIOSMobile()
25
+ ? 'ca-app-pub-3940256099942544/3986624511'
26
+ : 'ca-app-pub-3940256099942544/2247696110';
27
+ var factoryId = widget.control.getString("factory_id");
28
+ var templateStyle = parseNativeTemplateStyle(
29
+ widget.control.get("template_style"), Theme.of(context));
30
+ if (factoryId == null && templateStyle == null) {
31
+ return const ErrorControl("factory_id or template_style is required");
32
+ }
33
+
34
+ NativeAd nativeAd = NativeAd(
35
+ adUnitId: widget.control.getString("unit_id", testAdUnitId)!,
36
+ factoryId: factoryId,
37
+ listener: NativeAdListener(
38
+ onAdLoaded: (ad) {
39
+ widget.control.triggerEvent("load");
40
+ setState(() {
41
+ _isLoaded = true;
42
+ });
43
+ },
44
+ onAdFailedToLoad: (ad, error) {
45
+ widget.control.triggerEvent("error", error.toString());
46
+ ad.dispose(); // Dispose the ad here to free resources
47
+ },
48
+ onAdClicked: (ad) => widget.control.triggerEvent("click"),
49
+ onAdImpression: (ad) => widget.control.triggerEvent("impression"),
50
+ onAdClosed: (ad) => widget.control.triggerEvent("close"),
51
+ onAdOpened: (ad) => widget.control.triggerEvent("open"),
52
+ onAdWillDismissScreen: (ad) =>
53
+ widget.control.triggerEvent("will_dismiss"),
54
+ onPaidEvent: (Ad ad, valueMicros, precision, currencyCode) {
55
+ widget.control.triggerEvent("paid", {
56
+ "value": valueMicros,
57
+ "precision": precision,
58
+ "currency_code": currencyCode
59
+ });
60
+ },
61
+ ),
62
+ request:
63
+ parseAdRequest(widget.control.get("request"), const AdRequest())!,
64
+ nativeTemplateStyle: templateStyle);
65
+
66
+ if (!_isLoaded) {
67
+ nativeAd.load();
68
+ }
69
+
70
+ return ConstrainedControl(
71
+ control: widget.control, child: AdWidget(ad: nativeAd));
72
+ }
73
+ }
@@ -0,0 +1,14 @@
1
+ import 'package:google_mobile_ads/google_mobile_ads.dart';
2
+
3
+ AdRequest? parseAdRequest(dynamic value, [AdRequest? defaultValue]) {
4
+ if (value == null) return defaultValue;
5
+
6
+ return AdRequest(
7
+ keywords: value["keywords"],
8
+ contentUrl: value["content_url"],
9
+ nonPersonalizedAds: value["non_personalized_ads"],
10
+ neighboringContentUrls: value["neighboring_content_urls"],
11
+ httpTimeoutMillis: value["http_timeout"],
12
+ extras: value["extras"],
13
+ );
14
+ }
@@ -0,0 +1,51 @@
1
+ import 'package:collection/collection.dart';
2
+ import 'package:flet/flet.dart';
3
+ import 'package:flutter/material.dart';
4
+ import 'package:google_mobile_ads/google_mobile_ads.dart';
5
+
6
+ TemplateType? parseTemplateType(String? value, [TemplateType? defaultValue]) {
7
+ if (value == null) return defaultValue;
8
+ return TemplateType.values.firstWhereOrNull(
9
+ (e) => e.toString().toLowerCase() == value.toLowerCase()) ??
10
+ defaultValue;
11
+ }
12
+
13
+ NativeTemplateFontStyle? parseNativeTemplateFontStyle(String? value,
14
+ [NativeTemplateFontStyle? defaultValue]) {
15
+ if (value == null) return defaultValue;
16
+ return NativeTemplateFontStyle.values.firstWhereOrNull(
17
+ (e) => e.toString().toLowerCase() == value.toLowerCase()) ??
18
+ defaultValue;
19
+ }
20
+
21
+ NativeTemplateTextStyle? parseNativeTemplateTextStyle(
22
+ dynamic value, ThemeData theme,
23
+ [NativeTemplateTextStyle? defaultValue]) {
24
+ if (value == null) return defaultValue;
25
+
26
+ return NativeTemplateTextStyle(
27
+ size: parseDouble(value["size"]),
28
+ textColor: parseColor(value["color"], theme),
29
+ backgroundColor: parseColor(value["bgcolor"], theme),
30
+ style: parseNativeTemplateFontStyle(value["style"]),
31
+ );
32
+ }
33
+
34
+ NativeTemplateStyle? parseNativeTemplateStyle(dynamic value, ThemeData theme,
35
+ [NativeTemplateStyle? defaultValue]) {
36
+ if (value == null) return defaultValue;
37
+
38
+ return NativeTemplateStyle(
39
+ templateType:
40
+ parseTemplateType(value["template_type"], TemplateType.medium)!,
41
+ mainBackgroundColor: parseColor(value["main_bgcolor"], theme),
42
+ cornerRadius: parseDouble(value["corner_radius"]),
43
+ callToActionTextStyle: parseNativeTemplateTextStyle(
44
+ theme, value["call_to_action_text_style"]),
45
+ primaryTextStyle:
46
+ parseNativeTemplateTextStyle(value["primary_text_style"], theme),
47
+ secondaryTextStyle:
48
+ parseNativeTemplateTextStyle(value["secondary_text_style"], theme),
49
+ tertiaryTextStyle:
50
+ parseNativeTemplateTextStyle(value["tertiary_text_style"], theme));
51
+ }