Files
gl_dao/lib/ad/ad.dart
2022-05-27 17:52:17 +08:00

171 lines
5.0 KiB
Dart

import 'package:flutter/services.dart';
typedef AdCallback = void Function(String id);
typedef AdErrorCallback = void Function(String id, int code, String message);
/// API for showing various types of advertisements
class AdSdk {
static int _channelId = 0;
static MethodChannel _methodChannel =
new MethodChannel("flutter_adsjm_plugin/method");
/// set user id on login and logout
// static void setUserId(String userId) {
// _methodChannel.invokeMethod("setUserId", {"userId": userId});
// }
/// show reward video ad
static void showRewardVideoAd(String adId,
{AdCallback onSjmAdTradeId,
AdCallback onSjmAdLoaded,
AdCallback onSjmAdShow,
AdCallback onSjmAdReward,
AdCallback onSjmAdClick,
AdCallback onSjmAdVideoComplete,
AdCallback onSjmAdClose,
AdErrorCallback onSjmAdError}) {
EventChannel eventChannel =
EventChannel("flutter_adsjm_plugin/event_rewardVideo");
eventChannel.receiveBroadcastStream().listen((event) {
switch (event["event"]) {
case "onSjmAdTradeId":
onSjmAdTradeId?.call(event["id"]);
break;
case "onSjmAdLoaded":
onSjmAdLoaded?.call(event["id"]);
break;
case "onSjmAdShow":
onSjmAdShow?.call(event["id"]);
break;
case "onSjmAdReward":
onSjmAdReward?.call(event["id"]);
break;
case "onSjmAdClick":
onSjmAdClick?.call(event["id"]);
break;
case "onSjmAdVideoComplete":
onSjmAdVideoComplete?.call(event["id"]);
break;
case "onSjmAdClose":
onSjmAdClose?.call(event["id"]);
break;
case "onSjmAdError":
onSjmAdError?.call(event["id"], event["code"], event["message"]);
break;
}
});
_methodChannel.invokeMethod(
"showRewardVideoAd", {"_channelId": "rewardVideo", "adId": adId});
}
/// show interstitial ad
static void loadInterstitalAd(String adId,
{AdCallback onSjmAdLoaded,
AdCallback onSjmAdShow,
AdCallback onSjmAdClicked,
AdCallback onSjmAdClosed,
AdErrorCallback onSjmAdError}) {
EventChannel eventChannel =
EventChannel("flutter_adsjm_plugin/event_interstital");
eventChannel.receiveBroadcastStream().listen((event) {
switch (event["event"]) {
case "onSjmAdLoaded":
onSjmAdLoaded?.call(event["id"]);
break;
case "onSjmAdShow":
onSjmAdShow?.call(event["id"]);
break;
case "onSjmAdClicked":
onSjmAdClicked?.call(event["id"]);
break;
case "onSjmAdClosed":
onSjmAdClosed?.call(event["id"]);
break;
case "onSjmAdError":
onSjmAdError?.call(event["id"], event["code"], event["message"]);
break;
}
});
_methodChannel.invokeMethod(
"loadInterstitalAd", {"_channelId": "interstital", "adId": adId});
}
static void loadH5contentAd(
String adId, String userId, String username, String userhead,
{AdCallback onIntegralNotEnough,
AdCallback onIntegralExpend,
AdCallback onFinishTasks,
AdCallback onGameExit,
AdCallback onSjmAdRewardFinish,
AdCallback onSjmAdReward,
AdCallback onSjmAdLoaded,
AdCallback onSjmAdTradeId,
AdCallback onSjmAdClick,
AdCallback onSjmUserBehavior,
AdErrorCallback onSjmAdError}) {
EventChannel eventChannel =
EventChannel("flutter_adsjm_plugin/event_h5content");
eventChannel.receiveBroadcastStream().listen((event) {
switch (event["event"]) {
case "onIntegralNotEnough":
onIntegralNotEnough?.call(event["id"]);
break;
case "onIntegralExpend":
onIntegralExpend?.call(event["id"]);
break;
case "onFinishTasks":
onFinishTasks?.call(event["id"]);
break;
case "onGameExit":
onGameExit?.call(event["id"]);
break;
case "onSjmAdRewardFinish":
onSjmAdRewardFinish?.call(event["id"]);
break;
case "onSjmAdClick":
onSjmAdClick?.call(event["id"]);
break;
case "onSjmAdReward":
onSjmAdReward?.call(event["id"]);
break;
case "onSjmAdLoaded":
onSjmAdLoaded?.call(event["id"]);
break;
case "onSjmAdTradeId":
onSjmAdTradeId?.call(event["id"]);
break;
case "onSjmUserBehavior":
onSjmUserBehavior?.call(event["id"]);
break;
case "onSjmAdError":
onSjmAdError?.call(event["id"], event["code"], event["message"]);
break;
}
});
_methodChannel.invokeMethod("loadH5contentAd", {
"_channelId": "h5content",
"adId": adId,
"userId": userId,
"username": username,
"userhead": userhead
});
}
static void loadVideocontentAd(String adId) {
_methodChannel.invokeMethod(
"loadVideocontentAd", {"_channelId": "videocontent", "adId": adId});
}
}