Flutter广告展示插件adpopcorn_flutter_sdk的使用
Flutter广告展示插件adpopcorn_flutter_sdk的使用
AdPopcorn SDK for Flutter
爱德帕普康 Flutter SDK
开始使用
此插件会自动导入 Android 和 iOS 上的 AdPopcorn SDK,并为每个原生函数提供相应的 Flutter 调用接口。
注意:尽管此插件只是对原生 SDK 的封装,但在项目配置及函数使用上仍需参考 AdPopcorn 联接指南。
已知问题
AdPopcorn 的 Android 和 iOS SDK 提供的功能有所不同。本插件在接口层支持了大部分功能,但当调用当前运行平台不支持的函数时,将在 Flutter 层抛出 UnimplementedError
。
以下表格列出了截至 2022 年 6 月 13 日各平台支持的函数:
功能名称 | Android | iOS |
---|---|---|
setUserId | O | O |
openOfferwall | O | O |
getEarnableTotalRewardInfo | O | O |
setOnAgreePrivacy | O | X |
setOnDisagreePrivacy | O | X |
setOnClosedOfferWallPage | O | X |
setOnWillOpenOfferWall | X | O |
setOnDidOpenOfferWall | X | O |
setOnWillCloseOfferWall | X | O |
setOnDidCloseOfferWall | X | O |
useFlagShowWhenLocked | O | X |
openCSPage | O | X |
loadPopupAd | O | X |
showPopupAd | O | X |
setAppKeyAndHashKey | X | O |
useIgaworksRewardServer | X | O |
setLogLevel | X | O |
请注意,表中未列出的所有其他功能可能由 AdPopcorn SDK 提供。
使用示例
以下是一个完整的示例代码,展示了如何在 Flutter 中使用 adpopcorn_flutter_sdk
插件。
示例代码
import 'dart:developer';
import 'dart:io';
import 'dart:math' as math;
import 'package:adpopcorn_flutter_sdk/adpopcorn_offerwall.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey =
GlobalKey<ScaffoldMessengerState>();
final random = math.Random();
final textControllerAppKey = TextEditingController();
final textControllerHashKey = TextEditingController();
final textControllerUserId = TextEditingController();
List<Widget>? widgetsByPlatform;
[@override](/user/override)
void initState() {
super.initState();
if (Platform.isAndroid) {
initAndroid();
} else if (Platform.isIOS) {
initIOS();
}
}
[@override](/user/override)
void dispose() {
textControllerAppKey.dispose();
textControllerHashKey.dispose();
textControllerUserId.dispose();
super.dispose();
}
void initAndroid() {
widgetsByPlatform = [
buildSetUserId(),
buildOpenOfferwall(),
buildGetEarnableTotalRewardInfo(),
buildOpenCSPage(),
buildLoadPopupAd(),
buildShowPopupAd(),
buildUseFlagShowWhenLocked(),
];
AdPopcornOfferwall.setOnAgreePrivacy(
() => showSnackBar('onAgreePrivacy()'));
AdPopcornOfferwall.setOnDisagreePrivacy(
() => showSnackBar('onDisagreePrivacy()'));
AdPopcornOfferwall.setOnClosedOfferWallPage(
() => showSnackBar('onClosedOfferWallPage()'));
}
void initIOS() {
widgetsByPlatform = [
buildSetAppKeyHashKey(),
buildSetUserId(),
buildOpenOfferwall(),
buildGetEarnableTotalRewardInfo(),
buildUseIgaworksRewardServer(),
buildSetLogLevel(),
];
AdPopcornOfferwall.setOnWillOpenOfferWall(
() => showSnackBar('setOnWillOpenOfferWall()'));
AdPopcornOfferwall.setOnDidOpenOfferWall(
() => showSnackBar('setOnDidOpenOfferWall()'));
AdPopcornOfferwall.setOnWillCloseOfferWall(
() => showSnackBar('setOnWillCloseOfferWall()'));
AdPopcornOfferwall.setOnDidCloseOfferWall(
() => showSnackBar('setOnDidCloseOfferWall()'));
}
void showSnackBar(String text) {
scaffoldMessengerKey.currentState?.showSnackBar(SnackBar(
content: Text(text),
duration: const Duration(milliseconds: 600),
));
}
[@override](/user/override)
Widget build(BuildContext context) {
final List<Widget> children = [];
for (Widget widget in widgetsByPlatform!) {
children.add(const SizedBox(height: 8));
children.add(widget);
}
return MaterialApp(
scaffoldMessengerKey: scaffoldMessengerKey,
home: Scaffold(
appBar: AppBar(
title: const Text('AdPopcorn Flutter SDK 示例'),
),
body: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
child: Center(
child: Align(
alignment: Alignment.topCenter,
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
const SizedBox(height: 24),
for (final child in children) child,
],
),
),
),
),
),
),
);
}
Widget buildSetAppKeyHashKey() {
return Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 100,
child: TextFormField(
controller: textControllerAppKey,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: '应用密钥',
),
),
),
const SizedBox(width: 8),
SizedBox(
width: 100,
child: TextFormField(
controller: textControllerHashKey,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: '哈希密钥',
),
),
),
],
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () async {
final appKey = textControllerAppKey.text;
final hashKey = textControllerHashKey.text;
log('setAppKeyAndHashKey() appKey=$appKey, hashKey=$hashKey');
await AdPopcornOfferwall.setAppKeyAndHashKey(appKey, hashKey);
showSnackBar('setAppKeyAndHashKey()');
},
child: const Text('设置应用密钥和哈希密钥'),
),
],
);
}
Widget buildUseIgaworksRewardServer() {
return ElevatedButton(
onPressed: () async {
final flag = random.nextBool();
log('useIgaworksRewardServer() flag=$flag');
await AdPopcornOfferwall.useIgaworksRewardServer(flag);
showSnackBar('useIgaworksRewardServer($flag)');
},
child: const Text('启用 Igaworks 奖励服务器'),
);
}
Widget buildSetLogLevel() {
return ElevatedButton(
onPressed: () async {
final index = random.nextInt(AdPopcornLogLevel.values.length);
final level = AdPopcornLogLevel.values[index];
log('setLogLevel() level=$level');
await AdPopcornOfferwall.setLogLevel(level);
showSnackBar('setLogLevel(${describeEnum(level)})');
},
child: const Text('设置日志级别'),
);
}
Widget buildSetUserId() {
return Column(
children: [
SizedBox(
width: 200,
child: TextFormField(
controller: textControllerUserId,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: '用户 ID',
),
),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () async {
log('setUserId() id=${textControllerUserId.text}');
await AdPopcornOfferwall.setUserId(textControllerUserId.text);
showSnackBar('setUserId(${textControllerUserId.text})');
},
child: const Text('设置用户 ID'),
),
],
);
}
Widget buildOpenOfferwall() {
return ElevatedButton(
onPressed: () async {
log('openOfferWall()');
await AdPopcornOfferwall.openOfferWall();
},
child: const Text('打开奖励墙'),
);
}
Widget buildUseFlagShowWhenLocked() {
return ElevatedButton(
onPressed: () async {
final flag = random.nextBool();
log('useFlagShowWhenLocked() flag=$flag');
await AdPopcornOfferwall.useFlagShowWhenLocked(flag);
showSnackBar('useFlagShowWhenLocked($flag)');
},
child: const Text('启用锁定时显示广告'),
);
}
Widget buildOpenCSPage() {
return ElevatedButton(
onPressed: () async {
log('openCSPage() userId=${textControllerUserId.text}');
await AdPopcornOfferwall.openCSPage(textControllerUserId.text);
},
child: const Text('打开客服页面'),
);
}
Widget buildGetEarnableTotalRewardInfo() {
return ElevatedButton(
onPressed: () async {
log('getEarnableTotalRewardInfo()');
await AdPopcornOfferwall.getEarnableTotalRewardInfo(
(queryResult, totalCount, totalReward) {
showSnackBar(
'onGetEarnableTotalRewardInfo() queryResult=$queryResult, totalCount=$totalCount, totalReward=$totalReward');
});
},
child: const Text('获取可赚取奖励信息'),
);
}
Widget buildLoadPopupAd() {
return ElevatedButton(
onPressed: () async {
log('loadPopupAd()');
await AdPopcornOfferwall.loadPopupAd(
onLoadPopupAdSuccess: () => showSnackBar('onLoadPopupAdSuccess()'),
onLoadPopupAdFailure: (errorCode, errorMessage) =>
showSnackBar('onLoadPopupAdFailure() errorCode=$errorCode, errorMessage=$errorMessage'),
onShowPopupAdSuccess: () => showSnackBar('onShowPopupAdSuccess()'),
onShowPopupAdFailure: (errorCode, errorMessage) =>
showSnackBar('onShowPopupAdFailure()) errorCode=$errorCode, errorMessage=$errorMessage'),
onPopupAdClose: () => showSnackBar('onPopupAdClose()'),
);
},
child: const Text('加载弹窗广告'),
);
}
Widget buildShowPopupAd() {
return ElevatedButton(
onPressed: () async {
log('showPopupAd()');
await AdPopcornOfferwall.showPopupAd();
},
child: const Text('显示弹窗广告'),
);
}
}
更多关于Flutter广告展示插件adpopcorn_flutter_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter广告展示插件adpopcorn_flutter_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
adpopcorn_flutter_sdk
是一个用于在 Flutter 应用中展示广告的插件,通常用于集成 AdPopcorn 广告平台的功能。以下是如何在 Flutter 项目中使用 adpopcorn_flutter_sdk
的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 adpopcorn_flutter_sdk
的依赖。
dependencies:
flutter:
sdk: flutter
adpopcorn_flutter_sdk: ^版本号
请将 ^版本号
替换为最新的版本号。你可以在 pub.dev 上查找最新的版本。
2. 获取 SDK 初始化所需的参数
在使用 adpopcorn_flutter_sdk
之前,你需要在 AdPopcorn 平台上注册你的应用,并获取 AppKey
和 HashKey
。这些信息将用于初始化 SDK。
3. 初始化 SDK
在你的 Flutter 应用启动时,初始化 adpopcorn_flutter_sdk
。通常可以在 main.dart
文件中进行初始化。
import 'package:adpopcorn_flutter_sdk/adpopcorn_flutter_sdk.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 AdPopcorn SDK
await AdPopcornFlutterSdk.initialize(
appKey: '你的AppKey',
hashKey: '你的HashKey',
);
runApp(MyApp());
}
4. 展示广告
adpopcorn_flutter_sdk
提供了多种广告类型,如横幅广告、插屏广告、激励视频广告等。以下是一些常见广告类型的展示方法:
横幅广告
import 'package:adpopcorn_flutter_sdk/adpopcorn_flutter_sdk.dart';
class BannerAdExample extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Banner Ad Example'),
),
body: Column(
children: [
Expanded(
child: Center(
child: Text('Your content here'),
),
),
AdPopcornBannerAd(
placementId: '你的横幅广告PlacementId',
adSize: AdSize.BANNER,
listener: (AdEvent event) {
switch (event) {
case AdEvent.loaded:
print('Banner Ad loaded');
break;
case AdEvent.failed:
print('Banner Ad failed to load');
break;
case AdEvent.clicked:
print('Banner Ad clicked');
break;
case AdEvent.closed:
print('Banner Ad closed');
break;
}
},
),
],
),
);
}
}
插屏广告
import 'package:adpopcorn_flutter_sdk/adpopcorn_flutter_sdk.dart';
class InterstitialAdExample extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Interstitial Ad Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
AdPopcornInterstitialAd.load(
placementId: '你的插屏广告PlacementId',
listener: (AdEvent event) {
switch (event) {
case AdEvent.loaded:
print('Interstitial Ad loaded');
AdPopcornInterstitialAd.show();
break;
case AdEvent.failed:
print('Interstitial Ad failed to load');
break;
case AdEvent.clicked:
print('Interstitial Ad clicked');
break;
case AdEvent.closed:
print('Interstitial Ad closed');
break;
}
},
);
},
child: Text('Show Interstitial Ad'),
),
),
);
}
}
激励视频广告
import 'package:adpopcorn_flutter_sdk/adpopcorn_flutter_sdk.dart';
class RewardedAdExample extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Rewarded Ad Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
AdPopcornRewardedAd.load(
placementId: '你的激励视频广告PlacementId',
listener: (AdEvent event, {String? rewardName, int? rewardAmount}) {
switch (event) {
case AdEvent.loaded:
print('Rewarded Ad loaded');
AdPopcornRewardedAd.show();
break;
case AdEvent.failed:
print('Rewarded Ad failed to load');
break;
case AdEvent.clicked:
print('Rewarded Ad clicked');
break;
case AdEvent.closed:
print('Rewarded Ad closed');
break;
case AdEvent.completed:
print('Rewarded Ad completed. Reward: $rewardName, Amount: $rewardAmount');
break;
}
},
);
},
child: Text('Show Rewarded Ad'),
),
),
);
}
}