admob_util.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import 'dart:async';
  2. import 'dart:io' show Platform;
  3. import 'package:flutter/foundation.dart' show kIsWeb;
  4. import 'package:google_mobile_ads/google_mobile_ads.dart';
  5. export 'package:google_mobile_ads/google_mobile_ads.dart';
  6. // Learn more about displaying interstitial ads:
  7. // https://developers.google.com/admob/flutter/interstitial
  8. InterstitialAd? _interstitialAd;
  9. String? _loadingInterstitialAdUnitId;
  10. void loadInterstitialAd(
  11. String iosAdUnitId,
  12. String androidAdUnitId,
  13. bool showTestAds,
  14. ) {
  15. if (kIsWeb) {
  16. print('AdMob is not supported on web.');
  17. return;
  18. }
  19. String adUnitId;
  20. if (Platform.isIOS) {
  21. adUnitId =
  22. showTestAds ? 'ca-app-pub-3940256099942544/4411468910' : iosAdUnitId;
  23. } else if (Platform.isAndroid) {
  24. adUnitId = showTestAds
  25. ? 'ca-app-pub-3940256099942544/1033173712'
  26. : androidAdUnitId;
  27. } else {
  28. print("AdMob is not supported on this platform.");
  29. return;
  30. }
  31. if (adUnitId == _loadingInterstitialAdUnitId) {
  32. // Already loading the same ad.
  33. return;
  34. }
  35. if (adUnitId == _interstitialAd?.adUnitId) {
  36. // The ad is already loaded.
  37. return;
  38. }
  39. _loadingInterstitialAdUnitId = adUnitId;
  40. InterstitialAd.load(
  41. adUnitId: adUnitId,
  42. request: AdRequest(),
  43. adLoadCallback: InterstitialAdLoadCallback(
  44. onAdLoaded: (InterstitialAd ad) {
  45. if (adUnitId == _loadingInterstitialAdUnitId) {
  46. _interstitialAd = ad;
  47. _loadingInterstitialAdUnitId = null;
  48. }
  49. },
  50. onAdFailedToLoad: (LoadAdError error) {
  51. print('Interstitial ad failed to load: $error');
  52. _loadingInterstitialAdUnitId = null;
  53. },
  54. ),
  55. );
  56. }
  57. Future<bool> showInterstitialAd() async {
  58. if (_interstitialAd == null) {
  59. print('Interstitial ad is not loaded.');
  60. // Return success even if the ad is not yet loaded.
  61. // The ad waits for the user, so the user never waits for the ad!
  62. // https://youtu.be/r2RgFD3Apyo?t=188
  63. return true;
  64. }
  65. final completer = Completer<bool>();
  66. _interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
  67. onAdDismissedFullScreenContent: (InterstitialAd ad) {
  68. ad.dispose();
  69. _interstitialAd = null;
  70. completer.complete(true);
  71. },
  72. onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) {
  73. print('$ad onAdFailedToShowFullScreenContent: $error');
  74. ad.dispose();
  75. _interstitialAd = null;
  76. completer.complete(false);
  77. },
  78. );
  79. _interstitialAd!.show();
  80. return completer.future;
  81. }
  82. void adMobRequestConsent() {
  83. if (kIsWeb) {
  84. print('AdMob is not supported on web.');
  85. return;
  86. }
  87. ConsentRequestParameters params = ConsentRequestParameters();
  88. ConsentInformation.instance.requestConsentInfoUpdate(params, () async {
  89. if (await ConsentInformation.instance.isConsentFormAvailable()) {
  90. loadForm();
  91. }
  92. }, (error) {});
  93. }
  94. void loadForm() {
  95. ConsentForm.loadConsentForm((consentForm) async {
  96. var status = await ConsentInformation.instance.getConsentStatus();
  97. if (status == ConsentStatus.required) {
  98. consentForm.show((error) {
  99. loadForm();
  100. });
  101. }
  102. }, (error) {});
  103. }
  104. Future<bool> checkConsentNotRequired() async {
  105. var status = await ConsentInformation.instance.getConsentStatus();
  106. return status == ConsentStatus.notRequired;
  107. }
  108. void adMobUpdateRequestConfiguration() {
  109. if (kIsWeb) {
  110. print('AdMob is not supported on web.');
  111. return;
  112. }
  113. final RequestConfiguration requestConfiguration = RequestConfiguration();
  114. MobileAds.instance.updateRequestConfiguration(requestConfiguration);
  115. }