94 lines
2.8 KiB
Dart
94 lines
2.8 KiB
Dart
// Author: fengshengxiong
|
|
// Date: 2024/6/25
|
|
// Description: 原生广告
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
|
import 'package:tone_snap/data/enum/ad_scenes.dart';
|
|
import 'package:tone_snap/data/models/ad_config_model.dart';
|
|
import 'package:tone_snap/data/storage/music_box.dart';
|
|
import 'package:tone_snap/res/themes/app_colors.dart';
|
|
import 'package:tone_snap/utils/log_util.dart';
|
|
import 'package:tone_snap/utils/obj_util.dart';
|
|
|
|
class LibraryNativeAdManager {
|
|
LibraryNativeAdManager._();
|
|
|
|
static final LibraryNativeAdManager _instance = LibraryNativeAdManager._();
|
|
|
|
factory LibraryNativeAdManager() => _instance;
|
|
|
|
/// 广告对象
|
|
NativeAd? nativeAd;
|
|
|
|
/// 广告是否已加载
|
|
var nativeAdIsLoaded = false.obs;
|
|
|
|
/// 加载广告
|
|
void loadNativeAd() {
|
|
AdConfigModel adConfigModel = MusicBox().getAdConfig();
|
|
if (adConfigModel.library != null && adConfigModel.library!.isNotEmpty) {
|
|
_loadAd(adConfigModel.library![0].identifier, AdScenes.library.name);
|
|
}
|
|
}
|
|
|
|
/// 加载广告
|
|
void _loadAd(String? adUnitId, String adScenes) async {
|
|
if (ObjUtil.isEmpty(adUnitId)) {
|
|
return;
|
|
}
|
|
nativeAd = NativeAd(
|
|
adUnitId: adUnitId!,
|
|
request: const AdRequest(),
|
|
listener: NativeAdListener(
|
|
onAdLoaded: (ad) {
|
|
LogUtil.d('$adScenes:${ad.adUnitId}原生广告加载完成');
|
|
nativeAdIsLoaded.value = true;
|
|
},
|
|
onAdFailedToLoad: (ad, error) {
|
|
LogUtil.e('$adScenes:${ad.adUnitId}原生广告加载失败: $error');
|
|
nativeAdIsLoaded.value = false;
|
|
loadNativeAd();
|
|
ad.dispose();
|
|
},
|
|
onAdClicked: (ad) {},
|
|
onAdImpression: (ad) {
|
|
|
|
},
|
|
onAdClosed: (ad) {},
|
|
onAdOpened: (ad) {},
|
|
onAdWillDismissScreen: (ad) {},
|
|
onPaidEvent: (ad, valueMicros, precision, currencyCode) {
|
|
|
|
},
|
|
),
|
|
nativeTemplateStyle: NativeTemplateStyle(
|
|
templateType: TemplateType.medium,
|
|
mainBackgroundColor: scaffoldBgColor,
|
|
cornerRadius: 8.0,
|
|
callToActionTextStyle: NativeTemplateTextStyle(
|
|
backgroundColor: seedColor,
|
|
textColor: Colors.white,
|
|
style: NativeTemplateFontStyle.bold,
|
|
size: 16.0,
|
|
),
|
|
primaryTextStyle: NativeTemplateTextStyle(
|
|
textColor: seedColor,
|
|
style: NativeTemplateFontStyle.bold,
|
|
size: 14.0,
|
|
),
|
|
secondaryTextStyle: NativeTemplateTextStyle(
|
|
textColor: Colors.white,
|
|
style: NativeTemplateFontStyle.italic,
|
|
size: 12.0,
|
|
),
|
|
tertiaryTextStyle: NativeTemplateTextStyle(
|
|
textColor: seedColor,
|
|
style: NativeTemplateFontStyle.normal,
|
|
size: 14.0,
|
|
),
|
|
),
|
|
)..load();
|
|
}
|
|
} |