flutter_ad_new 0.0.1 flutter_ad_new: ^0.0.1 copied to clipboard
flutter plugin with ad
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_ad/flutter_plugin_ad.dart';
import 'package:flutter_ad_example/page/banner_page.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: MyHome());
}
}
class MyHome extends StatefulWidget {
@override
_MyHomeState createState() => _MyHomeState();
}
class _MyHomeState extends State<MyHome> {
// 上线前找运营 要正式的appId和广告位id
//广告测试id
String appId = "100000";
//开屏
String posIdSplash = "166590612";
//插屏
String posIdInterstitial = "166590614";
//激励视频
String posIdRewardVideo = "166590616";
String _result = '';
String _adEvent = '';
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = await FlutterPluginAd.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// 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(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
SizedBox(height: 6),
Text('Running on: $_platformVersion'),
SizedBox(height: 6),
Text('Result: $_result'),
SizedBox(height: 6),
Text('onAdEvent: $_adEvent'),
SizedBox(height: 6),
ElevatedButton(
child: Text('初始化'),
onPressed: () {
init();
},
),
SizedBox(height: 6),
ElevatedButton(
child: Text('添加广告监听'),
onPressed: () {
setAdEvent();
},
),
SizedBox(height: 6),
ElevatedButton(
child: Text('请求权限'),
onPressed: () {
checkAndReqPermission();
},
),
SizedBox(height: 6),
ElevatedButton(
child: Text('展示开屏广告'),
onPressed: () {
showSplashAd();
},
),
SizedBox(height: 6),
ElevatedButton(
child: Text('展示插屏广告'),
onPressed: () {
showInterstitialAd();
},
),
SizedBox(height: 6),
ElevatedButton(
child: Text('展示激励视频广告'),
onPressed: () {
showRewardVideoAd("userid","额外参数");
},
),
SizedBox(height: 6),
ElevatedButton(
child: Text('展示 Banner 广告'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BannerPage(),
));
},
),
SizedBox(height: 6),
],
)),
);
}
/// 初始化广告 SDK
Future<void> init() async {
try {
bool result = await FlutterPluginAd.initAd(appId, isDebug: true);
_result = "广告SDK 初始化${result ? '成功' : '失败'}";
} on PlatformException catch (e) {
_result =
"广告SDK 初始化失败 code:${e.code} msg:${e.message} details:${e.details}";
}
setState(() {});
}
/// 请求权限
Future<void> checkAndReqPermission() async {
try {
bool result = await FlutterPluginAd.checkAndReqPermission();
_result = "广告SDK 权限请求${result ? '成功' : '失败'}";
} on PlatformException catch (e) {
_result =
"广告SDK 权限请求失败 code:${e.code} msg:${e.message} details:${e.details}";
}
setState(() {});
}
/// 设置广告监听
Future<void> setAdEvent() async {
setState(() {
_adEvent = '设置成功';
});
FlutterPluginAd.onEventListener((event) {
setState(() {
_adEvent = 'type:${event.eventType} msg:${event.msg}';
if(event.eventType == "onReward"){
// 激励视频广告奖励达成
}
});
});
}
/// 展示开屏广告
Future<void> showSplashAd() async {
try {
bool result = await FlutterPluginAd.showSplashAd(posIdSplash);
_result = "展示开屏广告${result ? '成功' : '失败'}";
} on PlatformException catch (e) {
_result = "展示开屏广告失败 code:${e.code} msg:${e.message} details:${e.details}";
}
setState(() {});
}
/// 展示插屏广告
Future<void> showInterstitialAd() async {
try {
bool result =
await FlutterPluginAd.showInterstitialAd(posIdInterstitial);
_result = "插屏广告${result ? '成功' : '失败'}";
} on PlatformException catch (e) {
_result = "插屏广告失败 code:${e.code} msg:${e.message} details:${e.details}";
}
setState(() {});
}
/// 展示激励视频广告
Future<void> showRewardVideoAd(String userId,String custom_data) async {
try {
bool result = await FlutterPluginAd.showRewardVideoAd(posIdRewardVideo,userId,custom_data);
_result = "激励视频广告${result ? '成功' : '失败'}";
print(_result);
} on PlatformException catch (e) {
_result = "激励视频广告失败 code:${e.code} msg:${e.message} details:${e.details}";
}
setState(() {});
}
}