cs_okspin_ad 0.0.2
cs_okspin_ad: ^0.0.2 copied to clipboard
OkSpin 广告 SDK
example/lib/main.dart
import 'dart:developer';
import 'package:cs_okspin_ad/cs_okspin_ad_listener.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:cs_okspin_ad/cs_okspin_ad.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with CsOkSpinAdListener {
final _csOkspinAdPlugin = CsOkspinAd();
var showIconView = false;
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
await _csOkspinAdPlugin.setOkSpinAdListener(this);
await _csOkspinAdPlugin.setDebug(true);
await _csOkspinAdPlugin.initialize("xxxxxxx");
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {});
}
/// 初始化成功
@override
void onInitSuccess() {
debugPrint("cs_okspin onInitSuccess");
}
/// 初始化失败
@override
void onInitFailed(int errCode, String errMsg) {
debugPrint("cs_okspin onInitFailed - code:$errCode; msg:$errMsg");
}
/// Placement 加载成功
@override
void onIconReady(String placement) {
debugPrint("cs_okspin onIconReady - placement:$placement");
showIconView = true;
setState(() {});
}
/// Placement 加载失败
@override
void onIconLoadFailed(String placement, int errCode, String errMsg) {
debugPrint("cs_okspin onIconLoadFailed - placement:$placement; code:$errCode; msg:$errMsg");
}
/// Placement 素材展示失败
@override
void onIconShowFailed(String placementId, int errCode, String errMsg) {
debugPrint("cs_okspin onIconShowFailed - placement:$placementId; code:$errCode; msg:$errMsg");
}
/// Placement 被点击
@override
void onIconClick(String placement) {
debugPrint("cs_okspin onIconClick - placement:$placement");
}
/// GSpace - Interactive Ads 页面被打开
@override
void onInteractiveOpen(String placement) {
debugPrint("cs_okspin onInteractiveOpen - placement:$placement");
}
/// GSpace - Interactive Ads 页面打开失败
@override
void onInteractiveOpenFailed(String placementId, int errCode, String errMsg) {
debugPrint("cs_okspin onInteractiveOpenFailed - placement:$placementId; code:$errCode; msg:$errMsg");
}
/// GSpace - Interactive Ads 页面被关闭
@override
void onInteractiveClose(String placement) {
debugPrint("cs_okspin onInteractiveClose - placement:$placement");
}
/// GSpace - Interactive Wall 页面被打开
@override
void onOfferWallOpen(String placementId) {
debugPrint("cs_okspin onOfferWallOpen - placement:$placementId");
}
/// GSpace - Interactive Wall 页面打开失败
@override
void onOfferWallOpenFailed(String placementId, int errCode, String errMsg) {
debugPrint("cs_okspin onOfferWallOpenFailed - placement:$placementId; code:$errCode; msg:$errMsg");
}
/// GSpace - Interactive Wall 页面关闭
@override
void onOfferWallClose(String placementId) {
debugPrint("cs_okspin onOfferWallClose - placement:$placementId");
}
/// GSpace 页面打开
@override
void onGSpaceOpen(String placementId) {
debugPrint("cs_okspin onGSpaceOpen - placement:$placementId");
}
/// GSpace 页面打开失败
@override
void onGSpaceOpenFailed(String placementId, int errCode, String errMsg) {
debugPrint("cs_okspin onGSpaceOpenFailed - placement:$placementId; code:$errCode; msg:$errMsg");
}
/// GSpace 页面关闭
@override
void onGSpaceClose(String placementId) {
debugPrint("cs_okspin onGSpaceClose - placement:$placementId");
}
/// 用户交互行为回调
/// INTERACTIVE_PLAY GSpace - Interactive Ads 页面互动
/// INTERACTIVE_CLICK GSpace - Interactive Ads 页面点击广告
/// OFFER_WALL_SHOW_DETAIL GSpace - Interactive Wall 页面展示Offer详情
/// OFFER_WALL_GET_TASK GSpace - Interactive Wall 页面领取Offer
@override
void onUserInteraction(String placementId, String interaction) {
debugPrint("cs_okspin onUserInteraction - placement:$placementId; interaction:$interaction");
}
@override
Widget build(BuildContext context) {
final children = <Widget>[
Positioned(
child: TextButton(
onPressed: () {
log("点击加载 icon");
_csOkspinAdPlugin.loadIcon("9268");
},
child: const Text("加载Icon", style: TextStyle(color: Colors.black, fontSize: 20)),
))
];
if (showIconView) {
log("加载 okspin icon");
children.add(
const Positioned(
right: 15,
bottom: 20,
width: 60,
height: 60,
child: AndroidView(
viewType: "com.cangshengnian.cs_okspin_ad/customAd",
creationParams: <String, dynamic>{
"placementId": "9268",
"width": 60,
"height": 60
},
creationParamsCodec: StandardMessageCodec(),
),
),
);
}
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Container(
width: 300,
height: 400,
color: Colors.pink,
child: Stack(
alignment: Alignment.center,
children: children,
),
),
),
);
}
}