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

Ads Implement

example/lib/main.dart

// Copyright 2022 Bitcoding

import 'dart:convert';

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, consectetur adipiscing elit, sed do eiusmod'
      'adipiscing diam donec adipiscing tristique. Urna porttitor rhoncus'
      'dolor purus non enim praesent. Pellentesque habitant morbi tristique'
      'senectus et netus. Risus ultricies tristique nulla aliquet enim tortor'
      'at.';

  Widget _nativeAd = Container(height: 0);
  Widget _nativeAd2 = Container(height: 0);
  Widget _nativeAd4 = 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) => {
                        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) => {
                                      showNativeAds(
                                        onAdLoadedCallback: (p0) {
                                          setState(() {
                                            _nativeAd = p0;
                                          });
                                        },
                                      ),
                                      showBannerAds(
                                        onAdLoadedCallback: (p0) {
                                          setState(() {
                                            _bannerAd = p0;
                                          });
                                        },
                                      ),
                                    })
                          },
                        /* */
                        // await incrementNativeSequence(),
                        /* showNativeAds(
                          size: const Size(double.infinity, 200),
                          onAdLoadedCallback: (p1) {
                            setState(() {
                              _nativeAd2 = p1;
                            });
                          },
                        ),*/
                      })
            });
  }

  @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(
                          callback: () => {print("Show Completed true")});
                      break;
                    case 'RewardedAd':
                      showRewardAds(
                          callback: () => {print("Show Completed true")},
                          onSkippedCallback: () => {print("Skipped Ad true")},
                          onFailedCallback: () => {
                                showIntraAds(
                                    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,
                SizedBox(height: 20),
                Text(placeholderText),
                SizedBox(height: 20),
                _nativeAd2,
                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(callback: () {});
                  },
                  child: const Text('INTRA ADS'),
                ),
                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: () {
                    showRewardAds(callback: () {});
                  },
                  child: const Text('REWARD ADS'),
                ),
                /*   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: () {
                    showNativeCustomAds(
                      size: const Size(double.infinity, 200),
                      onAdLoadedCallback: (p0) {
                        setState(() {
                          _nativeAd3 = p0;
                        });
                      },
                    );
                  },
                  child: const Text('SHOW FIXED CUSTOM NATIVE ADS'),
                ),*/
                /*SizedBox(height: 20),
                _nativeAd3,
                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: () {
                    showNativeGoogleAds(
                      size: const Size(double.infinity, 200),
                      onAdLoadedCallback: (p0) {
                        setState(() {
                          _nativeAd4 = p0;
                        });
                      },
                    );
                  },
                  child: const Text('SHOW FIXED GOOGLE NATIVE ADS'),
                ),*/
                SizedBox(height: 20),
                _nativeAd4,
                /*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: () {
                    showNativeAds(
                      size: const Size(double.infinity, 200),
                      onAdLoadedCallback: (p0) {
                        setState(() {
                          _nativeAd5 = p0;
                        });
                      },
                    );
                  },
                  child: const Text('NATIVE ADS'),
                ),*/
                /*SizedBox(height: 20),
                _nativeAd5,
                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: () {
                    showNativeCustomAds(
                      size: const Size(double.infinity, 200),
                      onAdLoadedCallback: (p0) {
                        setState(() {
                          _nativeAd6 = p0;
                        });
                      },
                    );
                  },
                  child: const Text('SHOW FIXED CUSTOM NATIVE ADS'),
                ),
                SizedBox(height: 20),
                _nativeAd6,
                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: () {
                    showNativeAds(
                      size: const Size(double.infinity, 200),
                      onAdLoadedCallback: (p0) {
                        setState(() {
                          _nativeAd7 = p0;
                        });
                      },
                    );
                  },
                  child: const Text('NATIVE ADS'),
                ),
                SizedBox(height: 20),
                _nativeAd7,
                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: () {
                    showNativeAds(
                      size: const Size(double.infinity, 200),
                      onAdLoadedCallback: (p0) {
                        setState(() {
                          _nativeAd8 = p0;
                        });
                      },
                    );
                  },
                  child: const Text('NATIVE ADS'),
                ),
                SizedBox(height: 20),
                _nativeAd8,
                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: () {
                    showNativeCustomAds(
                      size: const Size(double.infinity, 200),
                      onAdLoadedCallback: (p0) {
                        setState(() {
                          _nativeAd9 = p0;
                        });
                      },
                    );
                  },
                  child: const Text('SHOW FIXED CUSTOM NATIVE ADS'),
                ),
                SizedBox(height: 20),
                _nativeAd9,
                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: () {
                    showNativeAds(
                      size: const Size(double.infinity, 200),
                      onAdLoadedCallback: (p0) {
                        setState(() {
                          _nativeAd10 = p0;
                        });
                      },
                    );
                  },
                  child: const Text('NATIVE ADS'),
                ),
                SizedBox(height: 20),
                _nativeAd10,
                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: () {
                    showNativeAds(
                      size: const Size(double.infinity, 200),
                      onAdLoadedCallback: (p0) {
                        setState(() {
                          _nativeAd11 = p0;
                        });
                      },
                    );
                  },
                  child: const Text('NATIVE ADS'),
                ),
                SizedBox(height: 20),
                _nativeAd11,
                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: () {
                    showNativeCustomAds(
                      size: const Size(double.infinity, 200),
                      onAdLoadedCallback: (p0) {
                        setState(() {
                          _nativeAd12 = p0;
                        });
                      },
                    );
                  },
                  child: const Text('SHOW FIXED CUSTOM NATIVE ADS'),
                ),
                SizedBox(height: 20),
                _nativeAd12,
                SizedBox(height: 20),*/
              ]),
            ),
          ),
          bottomNavigationBar: _bannerAd,
        );
      }),
    );
  }
}