union_ad_ssgf/example/lib/home_page.dart
2024-10-23 17:03:18 +08:00

243 lines
7.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:union_ad_ssgf/event/ad_event_handler.dart';
import 'package:union_ad_ssgf_example/ads_config.dart';
import 'package:union_ad_ssgf/union_ad_ssgf.dart';
// 结果信息
String _result = '';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _adEvent = '';
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Union AD plugin'),
),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(10),
child: Column(
children: [
const SizedBox(height: 10),
Text('Result: $_result'),
const SizedBox(height: 10),
Text('onAdEvent: $_adEvent'),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
child: const Text('初始化'),
onPressed: () {},
),
const SizedBox(height: 20),
ElevatedButton(
child: const Text('请求跟踪授权'),
onPressed: () {
requestIDFA();
},
),
const SizedBox(height: 20),
ElevatedButton(
child: const Text('个性化广告'),
onPressed: () {
setPersonalizedAd(1);
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
child: const Text('开屏(Logo2)'),
onPressed: () {},
),
const SizedBox(height: 20),
ElevatedButton(
child: const Text('开屏(全屏)'),
onPressed: () {
showSplashAd();
setState(() {});
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
child: const Text('插屏广告'),
onPressed: () {
showInterstitialAd(AdsConfig.interstitialId);
},
),
const SizedBox(height: 20),
ElevatedButton(
child: const Text('插全屏广告'),
onPressed: () {
showInterstitialAd(
AdsConfig.interstitialFullScreenVideoId,
showFullScreenVideo: true,
);
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
child: const Text('插屏激励'),
onPressed: () {
showInterstitialAd(
AdsConfig.interstitialRewardVideoId,
showFullScreenVideo: true,
showRewardVideo: true,
);
},
),
const SizedBox(height: 20),
ElevatedButton(
child: const Text('激励视频'),
onPressed: () {
showRewardVideoAd();
},
),
],
),
const SizedBox(height: 20),
ElevatedButton(
child: const Text('信息流'),
onPressed: () {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => FeedPage(),));
},
),
const SizedBox(height: 20),
const Center(child: Text('👇🏻 Banner 广告 👇🏻')),
const SizedBox(height: 10),
// AdBannerWidget(
// posId: AdsConfig.bannerId,
// width: 300,
// height: 80,
// interval: 0,
// show: true,
// ),
const SizedBox(height: 10),
],
),
),
),
);
}
/// 设置广告监听
Future<void> setAdEvent() async {
setState(() {
_adEvent = '设置成功';
});
UnionAdSsgf.onEventListener((event) {
_adEvent = 'adId:${event.adId} action:${event.action}';
if (event is AdErrorEvent) {
// 错误事件
_adEvent += ' errCode:${event.errCode} errMsg:${event.errMsg}';
} else if (event is AdRewardEvent) {
// 激励事件
_adEvent +=
' transId:${event.transId} customData:${event.customData} userId:${event.userId}';
}
print('onEventListener:$_adEvent');
setState(() {});
});
}
/// 请求应用跟踪透明度授权
Future<void> requestIDFA() async {
bool result = await UnionAdSsgf.requestIDFA;
_adEvent = '请求广告标识符:$result';
setState(() {});
}
/// 设置个性化广告
/// [state] 0:不限制 1:限制
Future<void> setPersonalizedAd(int state) async {
bool result = await UnionAdSsgf.setPersonalizedState(state);
_adEvent = '设置个性化广告:$result';
setState(() {});
}
/// 展示插屏广告
Future<void> showInterstitialAd(
String posId, {
bool showFullScreenVideo = false,
bool showRewardVideo = false,
}) async {
// try {
// bool result = await UnionAdSsgf.showInterstitialAd(
// posId,
// showPopup: false,
// showFullScreenVideo: showFullScreenVideo,
// showRewardVideo: showRewardVideo,
// autoPlayMuted: false,
// autoPlayOnWifi: false,
// userId: 'userId',
// customData: 'showInterstitialAd customData',
// );
// _result = "展示插屏广告${result ? '成功' : '失败'}";
// } on PlatformException catch (e) {
// _result = "展示插屏广告失败 code:${e.code} msg:${e.message} details:${e.details}";
// }
setState(() {});
}
/// 展示激励视频广告
Future<void> showRewardVideoAd() async {
try {
bool result = await UnionAdSsgf.showRewardVideoAd(
AdsConfig.rewardVideoId,
playMuted: false,
customData: 'showRewardVideoAd customData',
userId: 'userId',
);
_result = "展示激励视频广告${result ? '成功' : '失败'}";
} on PlatformException catch (e) {
_result =
"展示激励视频广告失败 code:${e.code} msg:${e.message} details:${e.details}";
}
setState(() {});
}
}
/// 展示开屏广告
/// [logo] 展示如果传递则展示logo不传递不展示
Future<void> showSplashAd([String? logo]) async {
try {
bool result = await UnionAdSsgf.showSplashAd(
AdsConfig.splashId,
logo: logo,
fetchDelay: 3,
);
_result = "展示开屏广告${result ? '成功' : '失败'}";
} on PlatformException catch (e) {
_result = "展示开屏广告失败 code:${e.code} msg:${e.message} details:${e.details}";
}
}