信息流广告

信息流广告

信息流广告调用

class NativeExpressAdView extends StatelessWidget {
  final String adId;
  final double width;
  //最大高度,返回的广告位高度小于等于此高度。
  final double height;
  //设置背景色,部分联盟为白色,无法更改,建议设置背景色统一设置为白色
  final Color? adBackgroundColor;

  final AdCallback? onAdLoad;
  final AdCallback? onAdShow;
  final AdCallback? onAdClick;
  final AdCallback? onAdDislike;
  final AdCallback? onError;
  final AdExtraCallback? onAdRenderSuccess;
  final AdCallback? onAdRenderFail;

// nativeExpressAdViewDidLoad
// nativeExpressAdViewWillShow
// nativeExpressAdDidClick

// nativeExpressAdDislike

// nativeExpressAdDidLoadFail
// nativeExpressAdRenderFail

// nativeExpressAdRenderSuccess
  NativeExpressAdView(
      {Key? key,
      required this.adId,
      required this.width,
      required this.height,
      this.adBackgroundColor,
      this.onAdLoad,
      this.onAdShow,
      this.onAdClick,
      this.onAdDislike,
      this.onAdRenderSuccess,
      this.onAdRenderFail,
      this.onError})
      : super(key: key);

  
  Widget build(BuildContext context) {
    Widget nativeExpress;
    if (defaultTargetPlatform == TargetPlatform.android) {
      nativeExpress = AndroidView(
        viewType: 'com.zjad.adsdk/nativeExpress',
        creationParams: {
          "adId": adId,
          "width": width,
          "height": height,
          "adBackgroundColor": adBackgroundColor?.value,
        },
        creationParamsCodec: const StandardMessageCodec(),
        onPlatformViewCreated: _onPlatformViewCreated,
      );
    } else if (defaultTargetPlatform == TargetPlatform.iOS) {
      nativeExpress = UiKitView(
        viewType: 'com.zjad.adsdk/nativeExpress',
        creationParams: {
          "adId": adId,
          "width": width,
          "height": height,
          "adBackgroundColor": adBackgroundColor?.value,
        },
        creationParamsCodec: const StandardMessageCodec(),
        onPlatformViewCreated: _onPlatformViewCreated,
      );
    } else {
      nativeExpress = Text("Not supported");
    }
    return Container(
      width: width,
      height: height,
      child: nativeExpress,
    );
  }

  void _onPlatformViewCreated(int id) {
    EventChannel eventChannel =
        EventChannel("com.zjsdk.adsdk/native_express_event_$id");
    eventChannel.receiveBroadcastStream().listen((event) {
      print('Flutter.Listen--------');
      switch (event["event"]) {
        case "nativeExpressAdViewDidLoad":
          onAdLoad?.call("nativeExpressAdViewDidLoad", "");
          break;
        case "nativeExpressAdViewWillShow":
          onAdShow?.call("nativeExpressAdViewWillShow", "");
          break;
        case "nativeExpressAdRenderFail":
          onAdRenderFail?.call("nativeExpressAdRenderFail", "");
          break;
        case "nativeExpressAdDidClick":
          onAdClick?.call("nativeExpressAdDidClick", "");
          break;

        case "nativeExpressAdDislike":
          onAdDislike?.call("nativeExpressAdDislike", "");
          break;

        case "nativeExpressAdDidLoadFail":
          onError?.call("nativeExpressAdDidLoadFail", event["error"]);
          break;

        case "nativeExpressAdRenderSuccess":
          onAdRenderSuccess?.call("nativeExpressAdRenderSuccess", "",
              extraMap: event['extraMap']);
          break;
      }
    });
  }
}

信息流广告回调说明

//信息流广告加载成功
nativeExpressAdViewDidLoad

//信息流广告加载失败
nativeExpressAdDidLoadFail

//信息流广告渲染失败
nativeExpressAdRenderFail

//信息流广告渲染成功,广告的真实高度在此回调中返回,通过extraMap的adHeight字段获取
nativeExpressAdRenderSuccess

//信息流广告曝光回调
nativeExpressAdViewWillShow

//点击信息流广告关闭/不喜欢按钮回调
nativeExpressAdDislike

//点击信息流广告回调
nativeExpressAdDidClick