Skip to content

Commit

Permalink
♻️ 优化整理页面
Browse files Browse the repository at this point in the history
  • Loading branch information
BytesZero committed Oct 22, 2023
1 parent 25a62c5 commit b90864a
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 121 deletions.
45 changes: 45 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "flutter_pangle_ads",
"request": "launch",
"type": "dart"
},
{
"name": "flutter_pangle_ads (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "flutter_pangle_ads (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
},
{
"name": "example",
"cwd": "example",
"request": "launch",
"type": "dart"
},
{
"name": "example (profile mode)",
"cwd": "example",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "example (release mode)",
"cwd": "example",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
]
}
49 changes: 49 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import 'package:flutter/material.dart';
import 'package:flutter_pangle_ads/flutter_pangle_ads.dart';

import 'ads_config.dart';
import 'pages/home_page.dart';

void main() {
// 绑定引擎
WidgetsFlutterBinding.ensureInitialized();
setAdEvent();
init().then((value) {
if (value) {
FlutterPangleAds.showSplashAd(
AdsConfig.splashId,
logo: AdsConfig.logo,
timeout: 3.5,
);
}
});

// 启动
runApp(MyApp());
}
Expand All @@ -22,3 +35,39 @@ class _MyAppState extends State<MyApp> {
);
}
}

/// 初始化广告 SDK
Future<bool> init() async {
bool result = await FlutterPangleAds.initAd(
AdsConfig.appId,
directDownloadNetworkType: [
NetworkType.kNetworkStateMobile,
NetworkType.kNetworkStateWifi,
],
);
debugPrint("广告SDK 初始化${result ? '成功' : '失败'}");

// 打开个性化广告推荐
FlutterPangleAds.setUserExtData(personalAdsType: '1');
return result;
}

/// 设置广告监听
Future<void> setAdEvent() async {
FlutterPangleAds.onEventListener((event) {
debugPrint('adId:${event.adId} action:${event.action}');
if (event is AdErrorEvent) {
// 错误事件
debugPrint(' errCode:${event.errCode} errMsg:${event.errMsg}');
} else if (event is AdRewardEvent) {
// 激励事件
debugPrint(
' rewardType:${event.rewardType} rewardVerify:${event.rewardVerify} rewardAmount:${event.rewardAmount} rewardName:${event.rewardName} errCode:${event.errCode} errMsg:${event.errMsg} customData:${event.customData} userId:${event.userId}');
}
// 测试关闭 Banner(会员场景)
if (event.action == AdEventAction.onAdClosed &&
event.adId == AdsConfig.bannerId02) {
debugPrint('仅会员可以关闭广告');
}
});
}
163 changes: 42 additions & 121 deletions example/lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,23 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_pangle_ads/flutter_pangle_ads.dart';
import 'package:flutter_pangle_ads_example/theme/style.dart';
import 'feed_page.dart';

import '../ads_config.dart';

// 结果信息
String _result = '';

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
String _result = '';
String _adEvent = '';

@override
void initState() {
super.initState();
init().then((value) {
if (value) {
showSplashAd(AdsConfig.logo);
}
});
setAdEvent();
}

@override
Expand All @@ -40,77 +33,55 @@ class _HomePageState extends State<HomePage> {
child: Center(
child: Column(
children: [
SizedBox(height: 10),
Text('Result: $_result'),
SizedBox(height: 10),
Text('onAdEvent: $_adEvent'),
SizedBox(height: 20),
ElevatedButton(
child: Text('初始化'),
onPressed: () {
init();
},
kDivider,
ListTile(
dense: true,
title: Text('请求应用跟踪透明授权(iOS)'),
onTap: () => requestIDFA(),
),
SizedBox(height: 20),
ElevatedButton(
child: Text('请求应用跟踪透明度授权(仅 iOS)'),
onPressed: () {
requestIDFA();
},
kDivider,
ListTile(
dense: true,
title: Text('请求相关权限(Android)'),
onTap: () => requestPermissionIfNecessary(),
),
SizedBox(height: 20),
ElevatedButton(
child: Text('动态请求相关权限(仅 Android)'),
onPressed: () {
requestPermissionIfNecessary();
},
kDivider,
ListTile(
title: Text('开屏广告(Logo2)'),
onTap: () => showSplashAd(AdsConfig.logo2),
),
SizedBox(height: 20),
ElevatedButton(
child: Text('开屏广告(Logo2)'),
onPressed: () {
showSplashAd(AdsConfig.logo2);
},
),
SizedBox(height: 20),
ElevatedButton(
child: Text('开屏广告(全屏)'),
onPressed: () {
showSplashAd();
},
kDivider,
ListTile(
title: Text('开屏广告(全屏)'),
onTap: () => showSplashAd(),
),
SizedBox(height: 20),
ElevatedButton(
child: Text('新插屏视频广告'),
onPressed: () {
showFullScreenVideoAd(AdsConfig.newInterstitialId);
},
kDivider,
ListTile(
title: Text('新插屏视频广告'),
onTap: () =>
showFullScreenVideoAd(AdsConfig.newInterstitialId),
),
SizedBox(height: 20),
ElevatedButton(
child: Text('新插屏(半屏)广告'),
onPressed: () {
showFullScreenVideoAd(AdsConfig.newInterstitialId2);
},
kDivider,
ListTile(
title: Text('新插屏(半屏)广告'),
onTap: () =>
showFullScreenVideoAd(AdsConfig.newInterstitialId2),
),
SizedBox(height: 20),
ElevatedButton(
child: Text('激励视频广告'),
onPressed: () {
showRewardVideoAd();
},
kDivider,
ListTile(
title: Text('激励视频广告'),
onTap: () => showRewardVideoAd(),
),
SizedBox(height: 20),
ElevatedButton(
child: Text('激励视频广告(进阶)'),
onPressed: () {
showRewardVideoAd2();
},
kDivider,
ListTile(
title: Text('激励视频广告(进阶)'),
onTap: () => showRewardVideoAd2(),
),
SizedBox(height: 20),
ElevatedButton(
child: Text('信息流'),
onPressed: () {
kDivider,
ListTile(
title: Text('信息流'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
Expand Down Expand Up @@ -145,56 +116,6 @@ class _HomePageState extends State<HomePage> {
);
}

/// 初始化广告 SDK
Future<bool> init() async {
try {
bool result = await FlutterPangleAds.initAd(
AdsConfig.appId,
directDownloadNetworkType: [
NetworkType.kNetworkStateMobile,
NetworkType.kNetworkStateWifi,
],
);
_result = "广告SDK 初始化${result ? '成功' : '失败'}";

// 打开个性化广告推荐
FlutterPangleAds.setUserExtData(personalAdsType: '1');

setState(() {});
return result;
} on PlatformException catch (e) {
_result =
"广告SDK 初始化失败 code:${e.code} msg:${e.message} details:${e.details}";
}
setState(() {});
return false;
}

/// 设置广告监听
Future<void> setAdEvent() async {
setState(() {
_adEvent = '设置成功';
});
FlutterPangleAds.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 +=
' rewardType:${event.rewardType} rewardVerify:${event.rewardVerify} rewardAmount:${event.rewardAmount} rewardName:${event.rewardName} errCode:${event.errCode} errMsg:${event.errMsg} customData:${event.customData} userId:${event.userId}';
}
// 测试关闭 Banner(会员场景)
if (event.action == AdEventAction.onAdClosed &&
event.adId == AdsConfig.bannerId02) {
_adEvent += '仅会员可以关闭广告';
}
print('onEventListener:$_adEvent');
setState(() {});
});
}

/// 请求应用跟踪透明度授权
Future<void> requestIDFA() async {
bool result = await FlutterPangleAds.requestIDFA;
Expand Down
6 changes: 6 additions & 0 deletions example/lib/router/router.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:flutter/material.dart';

// 去对应的页面
void pushPage(BuildContext context, Widget page) {
Navigator.push(context, MaterialPageRoute(builder: (context) => page));
}
4 changes: 4 additions & 0 deletions example/lib/theme/style.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

import 'package:flutter/material.dart';
// 横线
const kDivider=const Divider(height: 0.5);
Empty file.

0 comments on commit b90864a

Please sign in to comment.