新闻资讯

新闻资讯

此章节将演示如何请求在Flutter环境下请求与展示新闻资讯

新闻资讯分为原生新闻资讯新闻资讯组件两种类型:

  • 原生新闻资讯是拉起一个新的AndroidActivity,接入相对简单,效率更高;

  • 新闻资讯组件可以集成在当前的页面中,使用更加灵活。

原生新闻资讯

调用ZJAndroid.newsAd方法请求原生新闻资讯并处理结果。其中请求失败与展示失败事件合并为了展示失败的Event:

class ZJAndroid {
  /// 原生展示新闻资讯
  static void newsAd(

      /// 广告位ID
      String posId,

      /// 用户ID,必填
      String userId, {
        /// 回调。具体事件见 example
        Function(ZJEvent ret)? newsAdListener,
      });
}

原生新闻资讯回调说明

ret.action说明
ZJEventAction.onAdError广告展示失败
ret.code:错误码,非错误事件为0
ret.msg:错误信息,非错误事件为空字符串
ZJEventAction.onAdRewardVerify满足任务条件,可以发奖(仅配置任务时在关闭前回调)
ZJEventAction.onAdClose广告展示成功并已关闭

原生新闻资接入示例

example/lib/news_ad.dart
/// 原生加载广告
void _loadNewsAd(String posId) {
  ZJAndroid.newsAd(
    // 广告位ID
    posId,
    // 用户ID,必填
    TestPosId.testUserId, 
    newsAdListener: (ret) {
      switch (ret.action) {
        // 广告错误
        case ZJEventAction.onAdError:
          Fluttertoast.showToast(msg: "新闻资讯错误:${ret.msg}");
          if (kDebugMode) {
            print("${ret.action}:${ret.code}-${ret.msg}");
          }
          break;
        // 满足任务条件,可以发奖(仅配置任务时在关闭前回调)
        case ZJEventAction.onAdRewardVerify:
          Fluttertoast.showToast(msg: "新闻资讯发奖");
          break;
        // 关闭
        case ZJEventAction.onAdClose:
          Fluttertoast.showToast(msg: "新闻资讯关闭");
          break;
        default:
          // ignore
      }
    }
  );
}

新闻资讯组件

相比原生接入,Widget接入更加灵活,可以直接嵌入到FlutterView中。但需要处理View的移除

注意

使用新闻资讯组件加载时,需要确保 Android 的MainActivity继承io.flutter.embedding.android.FlutterFragmentActivity,Flutter会默认继承 io.flutter.embedding.android.FlutterActivity。详见example/MainActivity

/// 新闻资讯视图
class ZJNewsAdView extends StatelessWidget {
  /// 广告位ID
  final String posId;

  /// 用户ID
  final String userId;

  /// 宽
  final double width;

  /// 高
  final double height;

  /// 回调
  final Function(ZJEvent ret)? newsAdListener;

  const ZJNewsAdView(this.posId,
      {Key? key,
        required this.width,
        required this.height,
        this.userId = "",
        this.newsAdListener})
      : super(key: key);
}

新闻资讯组件回调说明

ret.action说明
ZJEventAction.onAdError广告展示失败
ret.code:错误码,非错误事件为0
ret.msg:错误信息,非错误事件为空字符串
ZJEventAction.onAdRewardVerify满足任务条件,可以发奖(仅配置任务时在关闭前回调)
ZJEventAction.onAdClose广告展示成功并已关闭

新闻资讯组件接入示例

example/lib/news_ad.dart
child: ZJNewsAdView(
    // 广告位ID
    posId,
    // 用户ID,必填
    userId: TestPosId.testUserId,
    width: double.infinity,
    height: double.infinity,
    newsAdListener: (ret) {
      switch (ret.action) {
        // 广告错误
        case ZJEventAction.onAdError:
          Fluttertoast.showToast(msg: "新闻资讯错误:${ret.msg}");
          if (kDebugMode) {
            print("${ret.action}:${ret.code}-${ret.msg}");
          }
          _reset();
          break;
        // 满足任务条件,可以发奖(仅配置任务时回调)
        case ZJEventAction.onAdRewardVerify:
          Fluttertoast.showToast(msg: "新闻资讯发奖");
          break;
        // 关闭
        case ZJEventAction.onAdClose:
          Fluttertoast.showToast(msg: "新闻资讯关闭");
          break;
        default:
          // ignore
      }
    },
  ),