union_ad_ssgf/example/lib/home_page.dart
2024-11-25 11:54:02 +08:00

220 lines
6.5 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_example/ads_config.dart';
// 结果信息
String _result = '';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _adEvent = '';
@override
void initState() {
super.initState();
setAdEvent();
}
@override
Widget build(BuildContext context) {
TextStyle style = const TextStyle(fontSize: 12);
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: Text(
'初始化',
style: style,
),
onPressed: () {},
),
const SizedBox(height: 20),
ElevatedButton(
child: Text(
'请求跟踪授权',
style: style,
),
onPressed: () {
requestIDFA();
},
),
const SizedBox(height: 20),
ElevatedButton(
child: Text(
'个性化广告',
style: style,
),
onPressed: () {
setPersonalizedAd(1);
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
child: const Text('开屏(Logo2)'),
onPressed: () {
showSplashAd(AdsConfig.logo2);
setState(() {});
},
),
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 = '设置成功';
});
}
/// 请求应用跟踪透明度授权
Future<void> requestIDFA() async {
bool result = false;
_adEvent = '请求广告标识符:$result';
setState(() {});
}
/// 设置个性化广告
/// [state] 0:不限制 1:限制
Future<void> setPersonalizedAd(int state) async {
bool result = false;
_adEvent = '设置个性化广告:$result';
setState(() {});
}
/// 展示插屏广告
Future<void> showInterstitialAd(
String posId, {
bool showFullScreenVideo = false,
bool showRewardVideo = false,
}) async {
setState(() {});
}
/// 展示激励视频广告
Future<void> showRewardVideoAd() async {
try {
bool result = false;
_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 = false;
_result = "展示开屏广告${result ? '成功' : '失败'}";
} on PlatformException catch (e) {
_result = "展示开屏广告失败 code:${e.code} msg:${e.message} details:${e.details}";
}
}