google_applovin_unity_ads 2.3.42 copy "google_applovin_unity_ads: ^2.3.42" to clipboard
google_applovin_unity_ads: ^2.3.42 copied to clipboard

Ads Implement

example/lib/main.dart

// Copyright 2022 Bitcoding

import 'package:flutter/material.dart';
import 'package:get/route_manager.dart';
import 'package:google_applovin_unity_ads/google_applovin_unity_ads.dart';
import 'package:google_applovin_unity_ads_example/main_screen.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  MobileAds.instance.initialize();
  FacebookAds.instance.initialize();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final String placeholderText = 'Lorem ipsum dolor sit amet';

  Widget _nativeAd = Container(height: 0);
  Widget _bannerAd = Container(height: 0);

  @override
  void initState() {
    super.initState();
    Future.delayed(
        const Duration(seconds: 1),
        () => {
              fetchAdsSettings(
                      appVersionCode: 1,
                      settingsUrl:
                          "https://gitlab.com/prbitcoding/ads_setting/-/raw/main/ads.json",
                      keyName: "com.bitlinks.pakistan.iptv.m3ulist")
                  .then((value) => {
                        getAdsSettings()?.adsSequence?.clear(),
                        getAdsSettings()?.intraAdsSequence?.clear(),
                        getAdsSettings()?.bannerAdsSequence?.clear(),
                        getAdsSettings()?.openAdsSequence?.clear(),
                        getAdsSettings()?.rewardsAdsSequence?.clear(),
                        getAdsSettings()?.adsSequence?.add("app_lovin"),
                        getAdsSettings()?.intraAdsSequence?.add("app_lovin"),
                        getAdsSettings()?.rewardsAdsSequence?.add("app_lovin"),
                        getAdsSettings()?.openAdsSequence?.add("app_lovin"),
                        if ((getAdsSettings()?.appLovin?.sdkKey ?? "")
                            .isNotEmpty) {
                            AppLovinAds.instance
                                .initialize(
                                  getAdsSettings()?.appLovin?.sdkKey ?? "",
                                  getAdsSettings()
                                              ?.adSetting
                                              ?.isGeoedgeSdkFlag ==
                                          true
                                      ? (getAdsSettings()
                                              ?.adSetting
                                              ?.geoedgeSdkKey ??
                                          "")
                                      : "",
                                  getAdsSettings()
                                              ?.adSetting
                                              ?.isGeoedgeSdkFlag ==
                                          true
                                      ? (getAdsSettings()
                                              ?.adSetting
                                              ?.geoedgeSdkKeyTimeoutInSeconds ??
                                          0)
                                      : 0,
                                )
                                .then((value) => {
                                      loadIntraAds(),
                                      loadRewardAds(),
                                      showMediumNativeAds(
                                        customOptions: {
                                          "btn_colors": ["#00F0F0"],
                                          "btn_corner_radius": 18.5,
                                          "btn_text_color": "#8bc34ae0",
                                          "btn_gradient_orientation":
                                              GradientOrientation
                                                  .TOP_BOTTOM.index,
                                          "text_heading_colors": "#8bc34ae0",
                                          "text_body_colors": "#8bc34ae0",
                                          "text_store_colors": "#8bc34ae0",
                                          "text_fonts": "mooli_regular.ttf",
                                        },
                                        onAdLoadedCallback: (p0) {
                                          setState(() {
                                            _nativeAd = p0;
                                          });
                                        },
                                      ),
                                      showBannerAds(
                                        margin: const EdgeInsets.symmetric(
                                            vertical: 24),
                                        onAdLoadedCallback: (p0) {
                                          setState(() {
                                            _bannerAd = p0;
                                          });
                                        },
                                      ),
                                    })
                          },
                        initOpenAds(
                            context: Get.context!,
                            onOpenAdLoaded: () {
                              showOpenAds(Get.context!);
                            })
                      })
            });
  }

  @override
  void dispose() {
    super.dispose();
    destroyAd();
  }

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      home: Builder(builder: (BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Ads example app'),
            actions: <Widget>[
              PopupMenuButton<String>(
                onSelected: (String result) {
                  switch (result) {
                    case 'InterstitialAd':
                      showIntraAds(
                          context: context,
                          callback: () => {print("showIntraAds Is Completed")});
                      break;
                    case 'RewardedAd':
                      showRewardAds(
                          context: context,
                          callback: () => {print("Show Completed true")},
                          onSkippedCallback: () => {print("Skipped Ad true")},
                          onFailedCallback: () => {
                                showIntraAds(
                                    context: context,
                                    isCompulsory: true,
                                    callback: () => {
                                          print(
                                              "Show Completed onFailedCallback")
                                        })
                              });
                      break;
                    case 'MainScreen':
                      Navigator.push(context, MaterialPageRoute(
                        builder: (context) {
                          return const MainScreen();
                        },
                      ));
                      break;
                    default:
                      throw AssertionError('unexpected button: $result');
                  }
                },
                itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
                  const PopupMenuItem<String>(
                    value: 'InterstitialAd',
                    child: Text('InterstitialAd'),
                  ),
                  const PopupMenuItem<String>(
                    value: 'RewardedAd',
                    child: Text('RewardedAd'),
                  ),
                  const PopupMenuItem<String>(
                    value: 'MainScreen',
                    child: Text('MainScreen'),
                  )
                ],
              ),
            ],
          ),
          body: SafeArea(
            child: SingleChildScrollView(
              child: Column(children: [
                _nativeAd,
                const SizedBox(height: 20),
                Text(placeholderText),
                const SizedBox(height: 20),
                TextButton(
                  style: ButtonStyle(
                    overlayColor: MaterialStateProperty.resolveWith<Color?>(
                        (Set<MaterialState> states) {
                      if (states.contains(MaterialState.focused))
                        return Colors.red;
                      return null; // Defer to the widget's default.
                    }),
                  ),
                  onPressed: () {
                    showIntraAds(context: context, callback: () {});
                  },
                  child: const Text('INTRA ADS'),
                ),
                const SizedBox(height: 20),
                TextButton(
                  style: ButtonStyle(
                    overlayColor: MaterialStateProperty.resolveWith<Color?>(
                        (Set<MaterialState> states) {
                      if (states.contains(MaterialState.focused)) {
                        return Colors.red;
                      }
                      return null; // Defer to the widget's default.
                    }),
                  ),
                  onPressed: () {
                    initOpenAds(
                        context: Get.context!,
                        onOpenAdLoaded: () {
                          showOpenAds(Get.context!);
                        });
                  },
                  child: const Text('REWARD ADS'),
                )
              ]),
            ),
          ),
          bottomNavigationBar: _bannerAd,
        );
      }),
    );
  }
}