Skip to content

信息流

1. 主要API

1.1 信息流广告请求类

ApexAdxAdRequest

参数说明
pid广告位
userId媒体用户体系ID,可选,默认 null
options扩展参数,默认 null

1.2 信息流广告对象类

ApexAdxNativeAd

方法说明
ApexAdxNativeAd({required ApexAdxAdRequest request, required double width, required double height, required ApexAdxNativeListener<ApexAdxNativeAd> listener})初始化信息流广告对象,request 为广告请求对象必传,listener 信息流广告回调接口必传
fetchAd()发起广告加载
isAvailable()判断当前是否存在可展示的广告
fetchAppInfo()获取应用信息(仅Android),返回 AppInfo 对象,包含 appName、appVersion、developerName、privacyUrl 等字段
updateAdSize(Size size)更新广告实际大小
Size? getAdSize()获取填充的广告宽高
release()销毁并关闭广告

1.3 信息流广告Widget对象类

NativeAdWidget

方法说明
NativeAdWidget({Key? key, required ApexAdxNativeAd nativeAd, required double width, required double height, Map? nativeCustomViewConfig})构造信息流广告Widget对象,nativeAd 信息流广告对象类必传,width widget宽,height widget高度,nativeCustomViewConfig 自渲染视图配置可选
updateAdSize(Size size)更新广告Widget大小

1.4 信息流广告回调说明

ApexAdxNativeListener 信息流广告回调说明

回调方法说明
onAdFailedToLoad(ApexAdxError error)广告加载失败
onAdLoaded(ApexAdxNativeAd ad)广告加载成功
onAdDisplayed()广告开始展示
onAdTapped()广告被用户点击
onAdRendered(ApexAdxNativeAd ad)用户广告渲染成功(只针对模版渲染,此时 ad.adSize 已根据 width 动态更新)
onAdFailedToRender(ApexAdxError error)广告渲染失败(只针对模版渲染)
onAdRejected(String reason)dislike 回调,广告被关闭。有穿山甲广告源时,必须在该方法里移除广告

1.5 原生广告自渲染自定义配置类

CustomNativeAdConfig 信息流广告自渲染配置类

静态方法说明
rootView()广告根视图容器
iconView()广告主图标
mainAdView()主广告视图
titleView()广告标题
descriptView()广告描述
adLogoView()广告字样或者广告渠道图标
ctaButton()广告转化按钮
dislikeButton()关闭广告按钮
Map createNativeSubViewAttribute(double width, double height, {double x = 0, double y = 0, String backgroundColor = '', String textColor = '#000000', int fontSize = 0, int scaleType = 0, int textAlignment = 0, bool isCtaClick = false, bool pixel = false})创建自渲染广告子视图组件属性配置

createNativeSubViewAttribute 原生广告自定义组件属性配置

属性成员说明
double xx坐标,相对于父容器,默认值0
double yy坐标,相对于父容器,默认值0
double width宽,默认值0
double height高,默认值0
bool pixel是否使用像素单位,默认值false
String backgroundColor背景色(如 #FFFFFF),默认值空
String textColor文字颜色,默认值 #000000
int fontSize字体大小,默认0,系统默认
int scaleType缩放模式,0,1,2; iOS 0:UIViewContentModeScaleToFill 1:UIViewContentModeScaleAspectFit 2:UIViewContentModeCenter; Android 0:FIT_CENTER 1: FIT_XY 2:CENTER
int textAlignment文字对齐模式,0,1,2; iOS 0:NSTextAlignmentLeft 1:NSTextAlignmentCenter 2:NSTextAlignmentRight; Android 0:TEXT_ALIGNMENT_INHERIT 1:TEXT_ALIGNMENT_CENTER 2:TEXT_ALIGNMENT_TEXT_END
bool isCtaClick自定义转化点击,默认false

2. 示例代码

2.1 创建信息流广告对象

dart
ApexAdxAdRequest request = ApexAdxAdRequest(pid: pid);
ApexAdxNativeAd nativeAd = ApexAdxNativeAd(
  request: request,
  listener: listener,
  width: 300,
  height: 200,
);

2.2 设置监听回调

dart
class IApexAdxNativeListener extends ApexAdxNativeListener<ApexAdxNativeAd> {
  @override
  void onAdFailedToLoad(ApexAdxError error) {
    print('onAdFailedToLoad');
  }

  @override
  void onAdLoaded(ApexAdxNativeAd ad) {
    print('onAdLoaded');
  }

  @override
  void onAdDisplayed() {
    print('onAdDisplayed');
  }

  @override
  void onAdFailedToRender(ApexAdxError error) {
    print('onAdFailedToRender');
  }

  @override
  void onAdRendered(ApexAdxNativeAd ad) {
    print('onAdRendered');
  }

  @override
  void onAdTapped() {
    print('onAdTapped');
  }

  @override
  void onAdRejected(String reason) {
    print('onAdRejected: $reason');
  }
}

2.3 信息流广告加载

dart
nativeAd.fetchAd();

2.4 信息流广告自定义View展示

dart
bool isReady = await nativeAd.isAvailable();
if (!isReady) return;
NativeAdWidget nativeAdWidget = NativeAdWidget(
  nativeAd: ad,
  height: 350,
  width: 300,
  nativeCustomViewConfig: {
    CustomNativeAdConfig.rootView(): CustomNativeAdConfig.createNativeSubViewAttribute(
      300, 350,
      x: 50,
      y: 350,
      backgroundColor: '#FFFFFF',
    ),
    CustomNativeAdConfig.iconView(): CustomNativeAdConfig.createNativeSubViewAttribute(
      50, 50,
      x: 10, y: 40,
    ),
    CustomNativeAdConfig.titleView(): CustomNativeAdConfig.createNativeSubViewAttribute(
      200, 20,
      x: 60, y: 210,
      fontSize: 15,
    ),
    CustomNativeAdConfig.descriptView(): CustomNativeAdConfig.createNativeSubViewAttribute(
      230, 20,
      x: 60, y: 230, fontSize: 15,
    ),
    CustomNativeAdConfig.ctaButton(): CustomNativeAdConfig.createNativeSubViewAttribute(
      100, 50,
      x: 20, y: 265,
      fontSize: 15,
      textColor: "#FFFFFF",
      backgroundColor: "#2095F1",
    ),
    CustomNativeAdConfig.mainAdView(): CustomNativeAdConfig.createNativeSubViewAttribute(
      290, 200,
      x: 5, y: 5, backgroundColor: '#00000000',
    ),
    CustomNativeAdConfig.adLogoView(): CustomNativeAdConfig.createNativeSubViewAttribute(
      20, 10,
      x: 10, y: 10,
      backgroundColor: '#00000000',
    ),
    CustomNativeAdConfig.dislikeButton(): CustomNativeAdConfig.createNativeSubViewAttribute(
      20, 20,
      x: 260, y: 210,
    ),
  },
);

2.5 注意事项

重要提示

穿山甲信息流关闭时,需要在 onAdRejected(String reason) 回调方法里,主动 remove 掉广告,否则有些广告会无法关闭,影响收益。

ApexAdx SDK Documentation