这个插件的作用是在 Flutter 应用中集成 Google Mobile Ads SDK,通过 Google 的广告平台在应用中展示广告,从而实现应用的变现。没错就是你玩游戏的时候看到的那些魔性小广告。我们集成这个插件的目的就是为了在应用中展示广告,用户获取应用内积分用于购买虚拟物品,我们获取广告收益。
classGoogleAds{ /// 这是广告单元信息--测试阶段是写的假的-todo staticfinal adUnitId = GetPlatform.isAndroid ? 'ca-app-pub-3940256099942544/5224354917' : 'ca-app-pub-3940256099942544/1712485313'; /// 广告 static RewardedAd? _rewardedAd; /// 加载广告 static googleAdsLoad() async { RewardedAd.load( adUnitId: adUnitId, request: const AdRequest(), rewardedAdLoadCallback: RewardedAdLoadCallback( // Called when an ad is successfully received. onAdLoaded: (ad) { ad.fullScreenContentCallback = FullScreenContentCallback( // Called when the ad showed the full screen content. onAdShowedFullScreenContent: (ad) {}, // Called when an impression occurs on the ad. onAdImpression: (ad) {}, // Called when the ad failed to show full screen content. onAdFailedToShowFullScreenContent: (ad, err) { // Dispose the ad here to free resources. ad.dispose(); }, // Called when the ad dismissed full screen content. onAdDismissedFullScreenContent: (ad) { // Dispose the ad here to free resources. ad.dispose(); }, // Called when a click is recorded for an ad. onAdClicked: (ad) {});
print('$ad loaded.'); // Keep a reference to the ad so you can show it later. _rewardedAd = ad; }, // Called when an ad request failed. onAdFailedToLoad: (LoadAdError error) { print('RewardedAd failed to load: $error'); }, ), ); } /// 展示广告 static showAds(){ _rewardedAd?.show(onUserEarnedReward: (AdWithoutView ad, RewardItem rewardItem) { // Reward the user for watching an ad. }); } /// 销毁广告 static disposeAds() { _rewardedAd?.dispose(); } }