59 lines
1.9 KiB
Dart
59 lines
1.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
|
import 'package:news/cubits/appSystemSettingCubit.dart';
|
|
|
|
const AdRequest request = AdRequest(
|
|
//static
|
|
keywords: <String>['foo', 'bar'],
|
|
contentUrl: 'http://foo.com/bar.html',
|
|
nonPersonalizedAds: true,
|
|
);
|
|
RewardedAd? rewardedAd;
|
|
int _numRewardedLoadAttempts = 0;
|
|
int maxFailedLoadAttempts = 3;
|
|
|
|
void createGoogleRewardedAd(BuildContext context) {
|
|
if (context.read<AppConfigurationCubit>().rewardId() != "") {
|
|
RewardedAd.load(
|
|
adUnitId: context.read<AppConfigurationCubit>().rewardId()!,
|
|
request: request,
|
|
rewardedAdLoadCallback: RewardedAdLoadCallback(
|
|
onAdLoaded: (RewardedAd ad) {
|
|
rewardedAd = ad;
|
|
_numRewardedLoadAttempts = 0;
|
|
},
|
|
onAdFailedToLoad: (LoadAdError error) {
|
|
rewardedAd = null;
|
|
_numRewardedLoadAttempts += 1;
|
|
if (_numRewardedLoadAttempts <= maxFailedLoadAttempts) {
|
|
createGoogleRewardedAd(context);
|
|
}
|
|
},
|
|
));
|
|
}
|
|
}
|
|
|
|
void showGoogleRewardedAd(BuildContext context) {
|
|
if (context.read<AppConfigurationCubit>().rewardId() != "") {
|
|
if (rewardedAd == null) {
|
|
return;
|
|
}
|
|
rewardedAd!.fullScreenContentCallback = FullScreenContentCallback(
|
|
onAdShowedFullScreenContent: (RewardedAd ad) => debugPrint('ad onAdShowedFullScreenContent.'),
|
|
onAdDismissedFullScreenContent: (RewardedAd ad) {
|
|
ad.dispose();
|
|
createGoogleRewardedAd(context);
|
|
},
|
|
onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) {
|
|
ad.dispose();
|
|
createGoogleRewardedAd(context);
|
|
},
|
|
);
|
|
|
|
rewardedAd!.setImmersiveMode(true);
|
|
rewardedAd!.show(onUserEarnedReward: (AdWithoutView ad, RewardItem reward) {});
|
|
rewardedAd = null;
|
|
}
|
|
}
|