cs_adfly_ad 0.0.1 cs_adfly_ad: ^0.0.1 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();
}
// 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("xxxxx", "xxxxx");
// 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,
),
),
),
);
}
}