cs_adfly_ad 0.0.3 cs_adfly_ad: ^0.0.3 copied to clipboard
adfly 广告SDK,目前只提供互动广告
example/lib/main.dart
import 'dart:developer';
import 'package:cs_adfly_ad/cs_adfly_ad_listener.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:cs_adfly_ad/cs_adfly_ad.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 CsAdflyAdListener {
final _csAdflyAdPlugin = CsAdflyAd();
var showIconView = false;
@override
void initState() {
super.initState();
initPlatformState();
}
// Listen
@override
void onInteractiveError(String? unitId, int? errCode, String? errMsg) {
debugPrint("cs_adfly onError $unitId; $errCode; $errMsg");
}
@override
void onInteractiveAdLoaded(String? unitId) {
debugPrint("cs_adfly onAdLoaded $unitId");
}
@override
void onInteractiveAdLoadFailure(String? unitId, int? errCode, String? errMsg) {
debugPrint("cs_adfly onAdLoadFailure $unitId; $errCode; $errMsg");
}
@override
void onInteractiveAdClicked(String? unitId) {
debugPrint("cs_adfly onAdClicked $unitId");
}
@override
void onInteractiveAdImpression(String? unitId) {
debugPrint("cs_adfly onAdImpression $unitId");
}
@override
void onInteractiveAdClosed(String? unitId) {
debugPrint("cs_adfly onAdClosed $unitId");
}
// 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 _csAdflyAdPlugin.setAdflyAdListener(this);
await _csAdflyAdPlugin.initialize("CandyCASH", "9084c09830c7aed7b12d8cbea9b235b7");
// 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
onInitSuccess() {
log("cs_adfly onInitSuccess");
showIconView = true;
setState(() {});
}
@override
Widget build(BuildContext context) {
final children = <Widget>[
Positioned(
child: TextButton(
onPressed: () {
log("点击加载 icon");
},
child: const Text("加载Icon", style: TextStyle(color: Colors.black, fontSize: 20)),
))
];
if (showIconView) {
log("加载 Adfly icon");
children.add(
const Positioned(
bottom: 20,
right: 20,
width: 120,
height: 120,
child: AndroidView(
viewType: "com.cangshengnian.cs_adfly_ad/customAd",
creationParams: <String, dynamic>{
"unitId": "4683",
"width": 120,
"height": 120,
"showClose": true,
},
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,
),
),
),
);
}
}