تنسيقات الاعلانات
: App Open Ads
Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); Map? configuration = await AppLovinMAX.initialize(_sdkKey); if (configuration != null) { _isInitialized = true; } runApp(const MyApp());}
class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key);
@override State<MyApp> createState() => _MyAppState();}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver { @override void initState() { super.initState();
if (_isInitialized) { AppLovinMAX.setAppOpenAdListener(AppOpenAdListener( onAdLoadedCallback: (ad) {}, onAdLoadFailedCallback: (adUnitId, error) {}, onAdDisplayedCallback: (ad) { }, onAdDisplayFailedCallback: (ad, error) { AppLovinMAX.loadAppOpenAd(_appOpenAdUnitId); }, onAdClickedCallback: (ad) {}, onAdHiddenCallback: (ad) { AppLovinMAX.loadAppOpenAd(_appOpenAdUnitId); }, onAdRevenuePaidCallback: (ad) {}, ));
AppLovinMAX.loadAppOpenAd(_appOpenAdUnitId);
}
WidgetsBinding.instance.addObserver(this); }
@override void dispose() { WidgetsBinding.instance.removeObserver(this);
super.dispose(); }
@override Future<void> didChangeAppLifecycleState(AppLifecycleState state) async { switch (state) { case AppLifecycleState.resumed: await showAdIfReady(); break;
case AppLifecycleState.paused: case AppLifecycleState.inactive: case AppLifecycleState.detached: break; } }
Future<void> showAdIfReady() async { if (!_isInitialized) { return; }
bool isReady = (await AppLovinMAX.isAppOpenAdReady(_appOpenAdUnitId))!; if (isReady) { AppLovinMAX.showAppOpenAd(_appOpenAdUnitId); } else { AppLovinMAX.loadAppOpenAd(_appOpenAdUnitId); } }
⋮
}
:Banners
final String _ad_unit_id = Platform.isAndroid ? "<ANDROID_AD_UNIT_ID>" : "<IOS_AD_UNIT_ID>";
MaxAdView(adUnitId: _ad_unit_id,adFormat: AdFormat.banner,isAutoRefreshEnabled: true
listener: AdViewAdListener(onAdLoadedCallback: (ad) {⋮}, onAdLoadFailedCallback: (adUnitId, error) {⋮}, onAdClickedCallback: (ad) {⋮}, onAdExpandedCallback: (ad) {⋮}, onAdCollapsedCallback: (ad) {⋮}))
:Interstitials
final String _interstitial_ad_unit_id = Platform.isAndroid ? "ANDROID_INTER_AD_UNIT_ID" : "IOS_INTER_AD_UNIT_ID";
var _interstitialRetryAttempt = 0;void initializeInterstitialAds() {AppLovinMAX.setInterstitialListener(InterstitialListener( onAdLoadedCallback: (ad) {// Interstitial ad is ready to be shown. AppLovinMAX.isInterstitialReady(_interstitial_ad_unit_id) will now return 'true'print('Interstitial ad loaded from ' + ad.networkName);// Reset retry attempt_interstitialRetryAttempt = 0;},onAdLoadFailedCallback: (adUnitId, error) {// Interstitial ad failed to load// We recommend retrying with exponentially higher delays up to a maximum delay (in this case 64 seconds)_interstitialRetryAttempt = _interstitialRetryAttempt + 1;int retryDelay = pow(2, min(6, _interstitialRetryAttempt)).toInt();print('Interstitial ad failed to load with code ' + error.code.toString() + ' - retrying in ' + retryDelay.toString() + 's');Future.delayed(Duration(milliseconds: retryDelay * 1000), () {AppLovinMAX.loadInterstitial(_interstitial_ad_unit_id);});},onAdDisplayedCallback: (ad) {⋮},onAdDisplayFailedCallback: (ad, error) {⋮},onAdClickedCallback: (ad) {⋮},onAdHiddenCallback: (ad) {⋮},));// Load the first interstitialAppLovinMAX.loadInterstitial(_interstitial_ad_unit_id);}
Showing an Interstitial Ad
To show an interstitial ad, call
showInterstitial()
:
bool isReady = (await AppLovinMAX.isInterstitialReady(_interstitial_ad_unit_id))!;
if (isReady) {AppLovinMAX.showInterstitial(_interstitial_ad_unit_id);}
:MRECs
final String _ad_unit_id = Platform.isAndroid ? "<ANDROID_AD_UNIT_ID>" : "<IOS_AD_UNIT_ID>";
MaxAdView(adUnitId: _ad_unit_id,adFormat: AdFormat.mrec,isAutoRefreshEnabled: true,
listener: AdViewAdListener(onAdLoadedCallback: (ad) {⋮}, onAdLoadFailedCallback: (adUnitId, error) {⋮}, onAdClickedCallback: (ad) {⋮}, onAdExpandedCallback: (ad) {⋮}, onAdCollapsedCallback: (ad) {⋮}))
:Native Ads
Container( margin: const EdgeInsets.all(8.0), height: 300, child: MaxNativeAdView( adUnitId: widget.adUnitId, controller: _nativeAdViewController, listener: NativeAdListener(onAdLoadedCallback: (ad) { logStatus('Native ad loaded from ${ad.networkName}'); setState(() { _mediaViewAspectRatio = ad.nativeAd?.Mediacontentaspectratio ?? _kMediaViewAspectRatio; }); }, onAdLoadFailedCallback: (adUnitId, error) { logStatus('Native ad failed to load with error code ${error.code} and message: ${error.message}'); }, onAdClickedCallback: (ad) { logStatus('Native ad clicked'); }, onAdRevenuePaidCallback: (ad) { logStatus('Native ad revenue paid: ${ad.revenue}'); }), child: Container( color: const Color(0xffefefef), padding: const EdgeInsets.all(8.0), child: Column( mainAxisSize: MainAxisSize.min, children: [ Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Container( padding: const EdgeInsets.all(4.0), child: const MaxNativeAdIconView( width: 48, height: 48, ), ), Flexible( child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ MaxNativeAdTitleView( style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16), maxLines: 1, overflow: TextOverflow.visible, ), MaxNativeAdAdvertiserView( style: TextStyle(fontWeight: FontWeight.normal, fontSize: 10), maxLines: 1, overflow: TextOverflow.fade, ), MaxNativeAdStarRatingView( size: 10, ), ], ), ), const MaxNativeAdOptionsView( width: 20, height: 20, ), ], ), Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Flexible( child: MaxNativeAdBodyView( style: TextStyle(fontWeight: FontWeight.normal, fontSize: 14), maxLines: 3, overflow: TextOverflow.ellipsis, ), ), ], ), const SizedBox(height: 8), Expanded( child: AspectRatio( aspectRatio: _mediaViewAspectRatio, child: const MaxNativeAdMediaView(), ), ), const SizedBox( width: double.infinity, child: MaxNativeAdCallToActionView( style: ButtonStyle( backgroundColor: MaterialStatePropertyAll<Color>(Color(0xff2d545e)), textStyle: MaterialStatePropertyAll<TextStyle>(TextStyle(fontSize: 20, fontWeight: FontWeight.bold)), ), ), ), ], ), ), ),),
:Rewarded Ads
final String _rewarded_ad_unit_id = Platform.isAndroid ? "ANDROID_REWARDED_AD_UNIT_ID" : "IOS_REWARDED_AD_UNIT_ID";
var _rewardedAdRetryAttempt = 0;void initializeRewardedAd() {AppLovinMAX.setRewardedAdListener(RewardedAdListener(onAdLoadedCallback: (ad) {// Rewarded ad is ready to be shown. AppLovinMAX.isRewardedAdReady(_rewarded_ad_unit_id) will now return 'true'print('Rewarded ad loaded from ' + ad.networkName);// Reset retry attempt_rewardedAdRetryAttempt = 0;}, onAdLoadFailedCallback: (adUnitId, error) {// Rewarded ad failed to load// We recommend retrying with exponentially higher delays up to a maximum delay (in this case 64 seconds)_rewardedAdRetryAttempt = _rewardedAdRetryAttempt + 1;int retryDelay = pow(2, min(6, _rewardedAdRetryAttempt)).toInt();print('Rewarded ad failed to load with code ' + error.code.toString() + ' - retrying in ' + retryDelay.toString() + 's');Future.delayed(Duration(milliseconds: retryDelay * 1000), () {AppLovinMAX.loadRewardedAd(_rewarded_ad_unit_id);});}, onAdDisplayedCallback: (ad) {⋮}, onAdDisplayFailedCallback: (ad, error) {⋮}, onAdClickedCallback: (ad) {⋮}, onAdHiddenCallback: (ad) {⋮}, onAdReceivedRewardCallback: (ad, reward) {⋮}));}void loadRewardedAd() {AppLovinMAX.loadRewardedAd(_rewarded_ad_unit_id);}
To show a rewarded ad, call
ShowRewardedAd()
:
bool isReady = (await AppLovinMAX.isRewardedAdReady(_rewarded_ad_unit_id))!;
if (isReady) {AppLovinMAX.showRewardedAd(_rewarded_ad_unit_id);}