import 'dart:convert';
|
import 'dart:io';
|
import 'package:flutter/material.dart';
|
import 'package:flutter_unionad/flutter_unionad.dart';
|
import 'package:flutter_tencentad/flutter_tencentad.dart';
|
import '../model/common/adinfo_model.dart';
|
import 'config_util.dart';
|
import 'string_util.dart';
|
import 'ui_constant.dart';
|
|
//紧急联系人输入框确定事件
|
typedef OnAdCallback = void Function(bool success, String msg);
|
|
typedef OnRewardAdCallback = void Function(RewardAdStatus status, String msg);
|
|
enum RewardAdStatus { fail, click, ready, verify, close }
|
|
class AdUtil {
|
static AdinfoModel? splashAdInfo;
|
|
static Future init() async {
|
await CSJAdUtil.init();
|
await GDTAdUtil.init();
|
splashAdInfo = await getAdInfo(AdPosition.splash);
|
}
|
|
static Widget loadSplash(AdinfoModel? adInfo, double width, double height,
|
OnAdCallback adCallback) {
|
if (adInfo == null) {
|
adCallback(false, "广告信息为空");
|
return Container();
|
}
|
|
if (StringUtil.isNullOrEmpty(adInfo.type)) {
|
adCallback(false, "广告类型为空");
|
return Container();
|
}
|
|
if (adInfo.type == "csj") {
|
return CSJAdUtil.loadSplash(adInfo.pid!, width, height, adCallback);
|
} else {
|
return GDTAdUtil.loadSplash(adInfo.pid!, width, height, adCallback);
|
}
|
}
|
|
static Widget? loadBanner(
|
AdinfoModel? adInfo, double width, double height, OnAdCallback callback) {
|
if (adInfo == null) {
|
callback(false, "广告信息为空");
|
return null;
|
}
|
|
if (StringUtil.isNullOrEmpty(adInfo.type)) {
|
callback(false, "广告类型为空");
|
return null;
|
}
|
|
if (adInfo.type == "csj") {
|
return CSJAdUtil.loadBanner(adInfo.pid!, width, height, callback);
|
} else {
|
return GDTAdUtil.loadBanner(adInfo.pid!, width, height, callback);
|
}
|
}
|
|
static Widget? loadExpress(
|
AdinfoModel? adInfo, double width, double height, OnAdCallback callback) {
|
if (adInfo == null) {
|
callback(false, "广告信息为空");
|
return null;
|
}
|
|
if (StringUtil.isNullOrEmpty(adInfo.type)) {
|
callback(false, "广告类型为空");
|
return null;
|
}
|
|
if (adInfo.type == "csj") {
|
return CSJAdUtil.loadExpress(adInfo.pid!, width, height, callback);
|
} else {
|
return GDTAdUtil.loadExpress(adInfo.pid!, width, height, callback);
|
}
|
}
|
|
static loadReward(AdinfoModel? adInfo, OnRewardAdCallback callback) async {
|
if (adInfo == null) {
|
callback(RewardAdStatus.fail, "广告信息为空");
|
return null;
|
}
|
|
if (StringUtil.isNullOrEmpty(adInfo.type)) {
|
callback(RewardAdStatus.fail, "广告类型为空");
|
return null;
|
}
|
|
if (adInfo.type == "csj") {
|
return CSJAdUtil.loadReward(adInfo.pid!, callback);
|
} else {
|
return GDTAdUtil.loadReward(adInfo.pid!, callback);
|
}
|
}
|
|
static loadInterstitial(AdinfoModel? adInfo, OnAdCallback callback) async {
|
if (adInfo == null) {
|
return null;
|
}
|
|
if (StringUtil.isNullOrEmpty(adInfo.type)) {
|
return null;
|
}
|
|
if (adInfo.type == "csj") {
|
CSJAdUtil.loadInterstitial(adInfo.pid!, callback);
|
} else {
|
GDTAdUtil.loadInterstitial(adInfo.pid!, callback);
|
}
|
}
|
|
static Future<AdinfoModel?> getAdInfo(String position) async {
|
var result = await ConfigUtil.getConfig(position);
|
if (result == null) {
|
return null;
|
}
|
AdinfoModel model = AdinfoModel.fromJson(jsonDecode(result));
|
if (StringUtil.isNullOrEmpty(model.type)) {
|
return null;
|
}
|
return model;
|
}
|
}
|
|
class CSJAdUtil {
|
static Future init() async {
|
var csjAppId = "5240078";
|
//初始化穿山甲
|
if (!StringUtil.isNullOrEmpty(csjAppId)) {
|
// if (Platform.isAndroid) {
|
// await FlutterUnionad.andridPrivacy(
|
// isCanUseLocation: false,
|
// //是否允许SDK主动使用地理位置信息 true可以获取,false禁止获取。默认为true
|
// lat: 1.0,
|
// //当isCanUseLocation=false时,可传入地理位置信息,穿山甲sdk使用您传入的地理位置信息lat
|
// lon: 1.0,
|
// //当isCanUseLocation=false时,可传入地理位置信息,穿山甲sdk使用您传入的地理位置信息lon
|
// isCanUsePhoneState: false,
|
// //是否允许SDK主动使用手机硬件参数,如:imei
|
// imei: "123",
|
// //当isCanUsePhoneState=false时,可传入imei信息,穿山甲sdk使用您传入的imei信息
|
// isCanUseWifiState: false,
|
// //是否允许SDK主动使用ACCESS_WIFI_STATE权限
|
// isCanUseWriteExternal: false,
|
// //是否允许SDK主动使用WRITE_EXTERNAL_STORAGE权限
|
// oaid: "111", //开发者可以传入oaid
|
// );
|
// }
|
|
await FlutterUnionad.register(
|
androidAppId: csjAppId,
|
//穿山甲广告 Android appid 必填
|
iosAppId: "",
|
//穿山甲广告 ios appid 必填
|
useTextureView: false,
|
//使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景,可以使用TextureView 选填
|
appName: Constant.APP_NAME,
|
//appname 必填
|
allowShowNotify: true,
|
//是否允许sdk展示通知栏提示 选填
|
allowShowPageWhenScreenLock: true,
|
//是否在锁屏场景支持展示广告落地页 选填
|
debug: true,
|
//是否显示debug日志
|
supportMultiProcess: true,
|
//是否支持多进程,true支持 选填
|
directDownloadNetworkType: [
|
FlutterUnionadNetCode.NETWORK_STATE_4G,
|
FlutterUnionadNetCode.NETWORK_STATE_WIFI
|
]); //允许直接下载的网络状态集合 选填
|
}
|
}
|
|
static Widget loadSplash(
|
String? pid, double width, double height, OnAdCallback adCallback) {
|
if (pid == null) {
|
adCallback(false, "pid为空");
|
return Container();
|
}
|
return FlutterUnionad.splashAdView(
|
//是否使用个性化模版 设定widget宽高
|
mIsExpress: true,
|
//android 开屏广告广告id 必填
|
androidCodeId: pid,
|
//ios 开屏广告广告id 必填
|
iosCodeId: "",
|
//是否支持 DeepLink 选填
|
supportDeepLink: true,
|
// 期望view 宽度 dp 选填 mIsExpress=true必填
|
expressViewWidth: width,
|
//期望view高度 dp 选填 mIsExpress=true必填
|
expressViewHeight: height,
|
callBack: FlutterUnionadSplashCallBack(
|
onShow: () {
|
print("开屏广告显示");
|
},
|
onClick: () {
|
print("开屏广告点击");
|
},
|
onFail: (error) {
|
adCallback(false, error);
|
},
|
onFinish: () {
|
print("开屏广告倒计时结束");
|
adCallback(true, "");
|
},
|
onSkip: () {
|
print("开屏广告跳过");
|
adCallback(true, "");
|
},
|
onTimeOut: () {
|
print("开屏广告超时");
|
adCallback(false, "开屏广告超时");
|
},
|
),
|
);
|
}
|
|
static Widget loadBanner(
|
String pid, double width, double height, OnAdCallback callback) {
|
return FlutterUnionad.bannerAdView(
|
//andrrid banner广告id 必填
|
androidCodeId: pid,
|
//ios banner广告id 必填
|
iosCodeId: "",
|
//是否使用个性化模版
|
mIsExpress: true,
|
//是否支持 DeepLink 选填
|
supportDeepLink: true,
|
//一次请求广告数量 大于1小于3 必填
|
expressAdNum: 3,
|
//轮播间隔事件 30-120秒 选填
|
expressTime: 30,
|
// 期望view 宽度 dp 必填
|
expressViewWidth: width,
|
//期望view高度 dp 必填
|
expressViewHeight: height,
|
//广告事件回调 选填
|
callBack: FlutterUnionadBannerCallBack(onShow: () {
|
print("banner广告加载完成");
|
}, onDislike: (message) {
|
print("banner不感兴趣 $message");
|
callback(false, "banner不感兴趣 $message");
|
}, onFail: (error) {
|
print("banner广告加载失败 $error");
|
}, onClick: () {
|
print("banner广告点击");
|
}),
|
);
|
}
|
|
static Widget loadExpress(
|
String pid, double width, double height, OnAdCallback callback) {
|
return FlutterUnionad.nativeAdView(
|
androidCodeId: pid,
|
//android 信息流广告id 必填
|
iosCodeId: "",
|
//ios banner广告id 必填
|
supportDeepLink: true,
|
//是否支持 DeepLink 选填
|
expressViewWidth: width,
|
// 期望view 宽度 dp 必填
|
expressViewHeight: height,
|
//期望view高度 dp 必填
|
expressNum: 1,
|
mIsExpress: true,
|
//一次请求广告数量 大于1小于3 必填
|
callBack: FlutterUnionadNativeCallBack(
|
onShow: () {
|
print("信息流广告显示");
|
},
|
onFail: (error) {
|
print("信息流广告失败 $error");
|
},
|
onDislike: (message) {
|
print("信息流广告不感兴趣 $message");
|
callback(false, "信息流广告不感兴趣 $message");
|
},
|
onClick: () {
|
print("信息流广告点击");
|
},
|
),
|
);
|
}
|
|
static loadReward(String pid, OnRewardAdCallback adCallback) async {
|
FlutterUnionad.loadRewardVideoAd(
|
mIsExpress: true,
|
//是否个性化 选填
|
androidCodeId: pid,
|
//Android 激励视频广告id 必填
|
iosCodeId: "",
|
//ios 激励视频广告id 必填
|
supportDeepLink: true,
|
//是否支持 DeepLink 选填
|
rewardName: "vip",
|
//奖励名称 选填
|
rewardAmount: 1,
|
//奖励数量 选填
|
userID: "",
|
// 用户id 选填
|
orientation: FlutterUnionadOrientation.VERTICAL,
|
//视屏方向 选填
|
mediaExtra: null, //扩展参数 选填
|
);
|
|
FlutterUnionadStream.initAdStream(
|
//激励广告
|
flutterUnionadRewardAdCallBack: FlutterUnionadRewardAdCallBack(
|
onShow: () {
|
print("激励广告显示");
|
},
|
onClick: () {
|
print("激励广告点击");
|
adCallback(RewardAdStatus.click, "激励广告点击");
|
},
|
onFail: (error) {
|
print("激励广告失败 $error");
|
adCallback(RewardAdStatus.fail, "激励广告失败 $error");
|
},
|
onClose: () {
|
print("激励广告关闭");
|
adCallback(RewardAdStatus.close, "激励广告关闭");
|
},
|
onSkip: () {
|
print("激励广告跳过");
|
},
|
onVerify: (bool isVerify, int rewardAmount, String rewardName,
|
int errorCode, String message) {
|
adCallback(RewardAdStatus.verify, "获取激励成功");
|
},
|
onReady: () async {
|
adCallback(RewardAdStatus.ready, "激励广告预加载准备就绪");
|
//显示激励广告
|
await FlutterUnionad.showRewardVideoAd();
|
},
|
onUnReady: () {
|
print("激励广告预加载未准备就绪");
|
},
|
),
|
);
|
}
|
|
static loadInterstitial(String pid, OnAdCallback adCallback) async {
|
FlutterUnionad.loadFullScreenVideoAdInteraction(
|
androidCodeId: pid, //android 全屏广告id 必填
|
iosCodeId: "", //ios 全屏广告id 必填
|
supportDeepLink: true, //是否支持 DeepLink 选填
|
orientation: FlutterUnionadOrientation.VERTICAL, //视屏方向 选填
|
);
|
|
FlutterUnionadStream.initAdStream(
|
// 新模板渲染插屏广告回调
|
flutterUnionadNewInteractionCallBack:
|
FlutterUnionadNewInteractionCallBack(
|
onShow: () {
|
print("新模板渲染插屏广告显示");
|
},
|
onSkip: () {
|
print("新模板渲染插屏广告跳过");
|
},
|
onClick: () {
|
print("新模板渲染插屏广告点击");
|
},
|
onFinish: () {
|
print("新模板渲染插屏广告结束");
|
},
|
onFail: (error) {
|
print("新模板渲染插屏广告错误 $error");
|
adCallback(false, "新模板渲染插屏广告错误 $error");
|
},
|
onClose: () {
|
print("新模板渲染插屏广告关闭");
|
adCallback(true, "新模板渲染插屏广告关闭");
|
},
|
onReady: () async {
|
print("新模板渲染插屏广告预加载准备就绪");
|
//显示新模板渲染插屏
|
await FlutterUnionad.showFullScreenVideoAdInteraction();
|
},
|
onUnReady: () {
|
print("新模板渲染插屏广告预加载未准备就绪");
|
},
|
),
|
);
|
}
|
}
|
|
class GDTAdUtil {
|
static Future init() async {
|
var gdtAppId = "1200343363";
|
//初始化广点通
|
if (!StringUtil.isNullOrEmpty(gdtAppId)) {
|
await FlutterTencentad.register(
|
androidId: gdtAppId, //androidId
|
iosId: "", //iosId
|
debug: true, //是否显示日志log
|
);
|
}
|
}
|
|
static Widget loadSplash(
|
String? pid, double width, double height, OnAdCallback adCallback) {
|
return FlutterTencentad.splashAdView(
|
//android广告id
|
androidId: pid!,
|
//ios广告id
|
iosId: "",
|
////设置开屏广告从请求到展示所花的最大时长(并不是指广告曝光时长),取值范围为[1500, 5000]ms
|
fetchDelay: 3500,
|
//广告回调
|
callBack: FlutterTencentadSplashCallBack(
|
onShow: () {
|
print("开屏广告显示");
|
},
|
onADTick: (time) {
|
print("开屏广告倒计时剩余时间 $time");
|
},
|
onClick: () {
|
print("开屏广告点击");
|
},
|
onClose: () {
|
print("开屏广告关闭");
|
adCallback(true, "开屏广告关闭");
|
},
|
onExpose: () {
|
print("开屏广告曝光");
|
},
|
onFail: (code, message) {
|
adCallback(false, message);
|
},
|
),
|
);
|
}
|
|
static Widget loadBanner(
|
String pid, double width, double height, OnAdCallback callback) {
|
return FlutterTencentad.bannerAdView(
|
//android广告id
|
androidId: pid,
|
//ios广告id
|
iosId: "",
|
//广告宽 单位dp
|
viewWidth: width,
|
//广告高 单位dp 宽高比应该为6.4:1
|
viewHeight: height,
|
// 广告回调
|
callBack: FlutterTencentadBannerCallBack(
|
onShow: () {
|
print("Banner广告显示");
|
},
|
onFail: (code, message) {
|
print("Banner广告错误 $code $message");
|
callback(false, "Banner广告错误 $code $message");
|
},
|
onClose: () {
|
print("Banner广告关闭");
|
callback(true, "Banner广告关闭");
|
},
|
onExpose: () {
|
print("Banner广告曝光");
|
},
|
onClick: () {
|
print("Banner广告点击");
|
},
|
),
|
);
|
}
|
|
static Widget loadExpress(
|
String pid, double width, double height, OnAdCallback callback) {
|
return FlutterTencentad.expressAdView(
|
//android广告id
|
androidId: pid,
|
//ios广告id
|
iosId: "",
|
//广告宽 单位dp
|
viewWidth: width.toInt(),
|
//广告高 单位dp
|
viewHeight: height.toInt(),
|
//回调事件
|
callBack: FlutterTencentadExpressCallBack(
|
onShow: () {
|
print("动态信息流广告显示");
|
},
|
onFail: (code, message) {
|
print("动态信息流广告错误 $code $message");
|
},
|
onClose: () {
|
print("动态信息流广告关闭");
|
},
|
onExpose: () {
|
print("动态信息流广告曝光");
|
},
|
onClick: () {
|
print("动态信息流广告点击");
|
},
|
));
|
}
|
|
static loadReward(String pid, OnRewardAdCallback adCallback) async {
|
await FlutterTencentad.loadRewardVideoAd(
|
//android广告id
|
androidId: pid,
|
//ios广告id
|
iosId: "",
|
//用户id
|
userID: "",
|
//奖励
|
rewardName: "VIP",
|
//奖励数
|
rewardAmount: 1,
|
//扩展参数 服务器回调使用
|
customData: "");
|
|
FlutterTencentAdStream.initAdStream(
|
//激励广告
|
flutterTencentadRewardCallBack:
|
FlutterTencentadRewardCallBack(onShow: () {
|
print("激励广告显示");
|
}, onClick: () {
|
print("激励广告点击");
|
adCallback(RewardAdStatus.click, "激励广告点击");
|
}, onFail: (code, message) {
|
print("激励广告失败 $code $message");
|
adCallback(RewardAdStatus.fail, "激励广告失败");
|
}, onClose: () {
|
print("激励广告关闭");
|
adCallback(RewardAdStatus.close, "激励广告关闭");
|
}, onReady: () async {
|
print("激励广告预加载准备就绪");
|
adCallback(RewardAdStatus.ready, "激励广告预加载准备就绪");
|
await FlutterTencentad.showRewardVideoAd();
|
}, onUnReady: () {
|
print("激励广告预加载未准备就绪");
|
}, onVerify: (transId, rewardName, rewardAmount) {
|
print("激励广告奖励 $transId $rewardName $rewardAmount");
|
adCallback(RewardAdStatus.verify, "激励广告奖励");
|
}, onFinish: () {
|
print("激励广告完成");
|
}),
|
);
|
}
|
|
static loadInterstitial(String pid, OnAdCallback adCallback) async {
|
await FlutterTencentad.loadUnifiedInterstitialAD(
|
//android广告id
|
androidId: pid,
|
//ios广告id
|
iosId: "",
|
//是否全屏
|
isFullScreen: false,
|
);
|
|
FlutterTencentAdStream.initAdStream(
|
flutterTencentadInteractionCallBack: FlutterTencentadInteractionCallBack(
|
onShow: () {
|
print("插屏广告显示");
|
},
|
onClick: () {
|
print("插屏广告点击");
|
},
|
onFail: (code, message) {
|
print("插屏广告失败 $code $message");
|
adCallback(false, "插屏广告失败 $code $message");
|
},
|
onClose: () {
|
print("插屏广告关闭");
|
adCallback(true, "插屏广告关闭");
|
},
|
onReady: () async {
|
print("插屏广告预加载准备就绪");
|
await FlutterTencentad.showUnifiedInterstitialAD();
|
},
|
onUnReady: () {
|
print("插屏广告预加载未准备就绪");
|
},
|
),
|
);
|
}
|
}
|
|
class AdPosition {
|
//开屏
|
static String splash = "splash";
|
|
//首页插屏
|
static String homeInterstitial = "homeInterstitial";
|
|
//我的页面原生
|
static String mineExpress = "mineExpress";
|
|
//搜索页面原生
|
static String searchExpress = "searchExpress";
|
|
//搜索结果banner
|
static String searchResultBanner = "searchResultBanner";
|
|
//轨迹分享页插屏
|
static String travelShareInterstitial = "travelShareInterstitial";
|
|
//会员激励视频
|
static String vipReward = "vipReward";
|
}
|