Flutter广告展示插件adpopcornssp_flutter的使用
Flutter广告展示插件adpopcornssp_flutter的使用
adpopcornssp_flutter
AdPopcornSSP 插件项目。
当前AdPopcornSSP SDK版本
- Android: 3.7.0
- iOS: 2.9.0
安装插件
在 pubspec.yaml
文件中添加依赖项:
dependencies:
adpopcornssp_flutter: ^1.0.8
集成指南
要查看更多信息,请参阅 AdPopcornSSP 集成指南。
完整示例代码
以下是一个完整的示例代码,展示了如何在 Flutter 应用程序中使用 adpopcornssp_flutter
插件来展示广告。
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io' show Platform;
import 'package:flutter/services.dart';
import 'package:adpopcornssp_flutter/adpopcornssp_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const MethodChannel androidBannerChannel = const MethodChannel('adpopcornssp/BANNER_320x50');
static const MethodChannel iosBannerChannel = const MethodChannel('adpopcornssp/iOS_BANNER_320x50');
static const MethodChannel androidNativeChannel = const MethodChannel('adpopcornssp/NATIVE_TEMPLATE');
static const MethodChannel iosNativeChannel = const MethodChannel('adpopcornssp/iOS_NATIVE_TEMPLATE');
[@override](/user/override)
void initState() {
super.initState();
if (Platform.isAndroid) {
AdPopcornSSP.init('663451319');
AdPopcornSSP.setUserId('TEST_ANDROID');
// 加载全屏广告
AdPopcornSSP.loadInterstitial('663451319', 'INTERSTITIAL');
AdPopcornSSP.interstitialAdLoadSuccessListener = (placementId) {
// AdPopcornSSP.showInterstitial('663451319', placementId);
};
// 加载全屏视频广告
AdPopcornSSP.loadInterstitialVideo('663451319', 'VIDEO');
AdPopcornSSP.interstitialVideoAdLoadSuccessListener = (placementId) {
//AdPopcornSSP.showInterstitialVideo('663451319', placementId);
};
// 加载奖励视频广告
AdPopcornSSP.loadRewardVideo('663451319', 'REWARD_VIDEO');
AdPopcornSSP.rewardVideoAdLoadSuccessListener = (placementId) {
//AdPopcornSSP.showRewardVideo('663451319', placementId);
};
// 打开内容广告(例如天气应用)
/*AdPopcornSSP.openContents('800296516', 'TEST_WEATHER');
AdPopcornSSP.contentsAdOpenSuccessListener = () {
print('main.dart contentsAdOpenSuccessListener');
};
AdPopcornSSP.contentsAdClosedListener = () {
print('main.dart contentsAdClosedListener');
};
AdPopcornSSP.contentsAdCompletedListener = (reward, rewardKey) {
print('main.dart contentsAdCompletedListener : ${rewardKey}');
};*/
// 配置事件处理方法
androidBannerChannel.setMethodCallHandler(_eventHandleMethod);
androidNativeChannel.setMethodCallHandler(_eventHandleMethod);
} else if (Platform.isIOS) {
AdPopcornSSP.init('397261446');
AdPopcornSSP.setUserId('TEST_IOS');
// 加载全屏广告
AdPopcornSSP.loadInterstitial('397261446', 'iOS_INTERSTITIAL');
AdPopcornSSP.interstitialAdLoadSuccessListener = (placementId) {
AdPopcornSSP.showInterstitial('397261446', placementId);
};
// 加载全屏视频广告
AdPopcornSSP.loadInterstitialVideo('397261446', 'iOS_VIDEO');
AdPopcornSSP.interstitialVideoAdLoadSuccessListener = (placementId) {
//AdPopcornSSP.showInterstitialVideo('397261446', placementId);
};
// 加载奖励视频广告
AdPopcornSSP.loadRewardVideo('397261446', 'iOS_REWARD_VIDEO');
AdPopcornSSP.rewardVideoAdLoadSuccessListener = (placementId) {
//AdPopcornSSP.showRewardVideo('397261446', placementId);
};
AdPopcornSSP.openContents('800296516', 'TEST_WEATHER');
AdPopcornSSP.contentsAdOpenSuccessListener = () { };
AdPopcornSSP.contentsAdClosedListener = () { };
AdPopcornSSP.contentsAdCompletedListener = (reward, rewardKey) {
print('main.dart contentsAdCompletedListener : ${rewardKey}');
};
// 配置事件处理方法
iosBannerChannel.setMethodCallHandler(_eventHandleMethod);
iosNativeChannel.setMethodCallHandler(_eventHandleMethod);
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('插件示例应用'),
),
body: _body(),
),
);
}
Widget _body() {
return ListView(
children: _listItem(),
);
}
List<Widget> _listItem() {
List<Widget> widgets = [];
widgets.add(_setBannerView());
widgets.add(_setNativeView());
return widgets;
}
Widget _setBannerView() {
const String viewType = 'AdPopcornSSPBannerView';
if (Platform.isAndroid) {
final Map<String, dynamic> creationParams = {'appKey': '663451319', 'placementId': 'BANNER_320x50', 'bannerSize': '320x50'};
return Container(
width: double.maxFinite,
height: 50,
child: AndroidView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
),
);
}
else if (Platform.isIOS) {
final Map<String, dynamic> creationParams = {'appKey': '397261446', 'placementId': 'iOS_BANNER_320x50', 'bannerSize': '320x50'};
return Container(
width: double.maxFinite,
height: 50,
child: UiKitView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
),
);
}
else{
return Container(
width: double.maxFinite,
height: 1
);
}
}
Widget _setNativeView() {
const String viewType = 'AdPopcornSSPNativeView';
if (Platform.isAndroid) {
final Map<String, dynamic> creationParams = {'appKey': '663451319', 'placementId': 'NATIVE_TEMPLATE'};
return Container(
width: double.maxFinite,
height: 200,
child: AndroidView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
),
);
}
else if (Platform.isIOS) {
final Map<String, dynamic> creationParams = {'appKey': '397261446', 'placementId': 'iOS_NATIVE_TEMPLATE'};
return Container(
width: double.maxFinite,
height: 280,
child: UiKitView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
),
);
}
else{
return Container(
width: double.maxFinite,
height: 1
);
}
}
static Future<dynamic> _eventHandleMethod(MethodCall call) async {
print('_eventHandleMethod: ${call.method}, ${call.arguments}');
final Map<dynamic, dynamic> arguments = call.arguments;
final String method = call.method;
final String placementId = arguments['placementId'];
if (method == 'APSSPBannerViewLoadSuccess') {
print('main.dart APSSPBannerViewLoadSuccess');
} else if (method == 'APSSPBannerViewLoadFail') {
final int errorCode = arguments['errorCode'];
print('main.dart APSSPBannerViewLoadFail');
} else if (method == 'APSSPBannerViewClicked') {
print('main.dart APSSPBannerViewClicked');
} else if (method == 'APSSPNativeAdLoadSuccess') {
print('main.dart APSSPNativeAdLoadSuccess');
} else if (method == 'APSSPNativeAdLoadFail') {
final int errorCode = arguments['errorCode'];
print('main.dart APSSPNativeAdLoadFail');
} else if (method == 'APSSPNativeAdImpression') {
print('main.dart APSSPNativeAdImpression');
} else if (method == 'APSSPNativeAdClicked') {
print('main.dart APSSPNativeAdClicked');
}
return Future.value(null);
}
}
更多关于Flutter广告展示插件adpopcornssp_flutter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter广告展示插件adpopcornssp_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter项目中使用adpopcornssp_flutter
插件来展示广告的示例代码。这个插件假设是用于展示AdPopcorn SSP(Supply-Side Platform)的广告。请注意,由于具体的插件实现和API可能会随着版本的更新而变化,以下代码可能需要根据实际插件文档进行调整。
首先,确保你已经在pubspec.yaml
文件中添加了adpopcornssp_flutter
依赖:
dependencies:
flutter:
sdk: flutter
adpopcornssp_flutter: ^最新版本号 # 替换为实际最新版本号
然后,运行flutter pub get
来获取依赖。
接下来,在你的Flutter项目中,你可以按照以下步骤来初始化并展示广告:
- 初始化插件:
在你的主文件(通常是main.dart
)或者合适的位置初始化插件。
import 'package:flutter/material.dart';
import 'package:adpopcornssp_flutter/adpopcornssp_flutter.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
// 初始化AdPopcorn SSP插件,这里可能需要配置一些初始化参数
AdPopcornSSPFlutter.instance.initialize(
appId: '你的AppId', // 替换为你的AppId
// 其他初始化参数,根据插件文档添加
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter AdPopcorn SSP Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
- 创建广告位:
在你的主页面或者其他页面中创建一个广告位来展示广告。
import 'package:flutter/material.dart';
import 'package:adpopcornssp_flutter/adpopcornssp_flutter.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
BannerAd? _bannerAd;
@override
void initState() {
super.initState();
// 创建广告位
_bannerAd = BannerAd(
adUnitId: '你的BannerAdUnitId', // 替换为你的Banner广告单元ID
adSize: AdSize.banner, // 或者其他适合你的广告尺寸
listener: (AdStatus status) {
// 广告状态监听,比如加载成功、失败等
print('Banner Ad status: $status');
},
)..load(); // 加载广告
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter AdPopcorn SSP Demo'),
),
body: Center(
child: _bannerAd?.widget ?? Container(
height: 50, // 占位高度,根据广告尺寸调整
child: Center(child: CircularProgressIndicator()), // 加载指示器
),
),
);
}
@override
void dispose() {
_bannerAd?.dispose(); // 释放广告资源
super.dispose();
}
}
- 处理广告事件(可选):
根据需求,你可能还需要处理其他广告事件,比如用户点击广告、广告关闭等。这些事件通常可以通过监听器(listener)来处理。
// 在BannerAd的listener中添加更多事件处理
listener: (AdStatus status) {
if (status == AdStatus.loaded) {
print('Banner Ad loaded successfully.');
} else if (status == AdStatus.failedToLoad) {
print('Failed to load Banner Ad.');
} else if (status == AdStatus.clicked) {
print('Banner Ad clicked.');
}
// 其他状态处理
},
请注意,上述代码只是一个基本示例,实际使用中你可能需要根据adpopcornssp_flutter
插件的具体API文档来调整代码。特别是初始化参数、广告单元ID、广告尺寸等都需要根据你的实际配置来设置。
此外,由于广告网络可能会更新其SDK和API,建议定期查看插件的官方文档和更新日志,以确保你的代码与最新版本的插件兼容。