From b90864abbb3b9a06615fe96d71b9ee8de3319152 Mon Sep 17 00:00:00 2001 From: Zero <1300326388@qq.com> Date: Sun, 22 Oct 2023 14:35:30 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=95=B4=E7=90=86=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 45 +++++++++ example/lib/main.dart | 49 ++++++++++ example/lib/pages/home_page.dart | 163 ++++++++----------------------- example/lib/router/router.dart | 6 ++ example/lib/theme/style.dart | 4 + example/lib/widgets/widgets.dart | 0 6 files changed, 146 insertions(+), 121 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 example/lib/router/router.dart create mode 100644 example/lib/theme/style.dart create mode 100644 example/lib/widgets/widgets.dart diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..95a27a2 --- /dev/null +++ b/.vscode/launch.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index f81f312..885f81a 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -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()); } @@ -22,3 +35,39 @@ class _MyAppState extends State { ); } } + +/// 初始化广告 SDK +Future init() async { + bool result = await FlutterPangleAds.initAd( + AdsConfig.appId, + directDownloadNetworkType: [ + NetworkType.kNetworkStateMobile, + NetworkType.kNetworkStateWifi, + ], + ); + debugPrint("广告SDK 初始化${result ? '成功' : '失败'}"); + + // 打开个性化广告推荐 + FlutterPangleAds.setUserExtData(personalAdsType: '1'); + return result; +} + +/// 设置广告监听 +Future 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('仅会员可以关闭广告'); + } + }); +} diff --git a/example/lib/pages/home_page.dart b/example/lib/pages/home_page.dart index 9d47ff4..f940cf8 100644 --- a/example/lib/pages/home_page.dart +++ b/example/lib/pages/home_page.dart @@ -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 { + String _result = ''; String _adEvent = ''; @override void initState() { super.initState(); - init().then((value) { - if (value) { - showSplashAd(AdsConfig.logo); - } - }); - setAdEvent(); } @override @@ -40,77 +33,55 @@ class _HomePageState extends State { 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( @@ -145,56 +116,6 @@ class _HomePageState extends State { ); } - /// 初始化广告 SDK - Future 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 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 requestIDFA() async { bool result = await FlutterPangleAds.requestIDFA; diff --git a/example/lib/router/router.dart b/example/lib/router/router.dart new file mode 100644 index 0000000..313358e --- /dev/null +++ b/example/lib/router/router.dart @@ -0,0 +1,6 @@ +import 'package:flutter/material.dart'; + +// 去对应的页面 +void pushPage(BuildContext context, Widget page) { + Navigator.push(context, MaterialPageRoute(builder: (context) => page)); +} diff --git a/example/lib/theme/style.dart b/example/lib/theme/style.dart new file mode 100644 index 0000000..34f2d95 --- /dev/null +++ b/example/lib/theme/style.dart @@ -0,0 +1,4 @@ + +import 'package:flutter/material.dart'; +// 横线 +const kDivider=const Divider(height: 0.5); \ No newline at end of file diff --git a/example/lib/widgets/widgets.dart b/example/lib/widgets/widgets.dart new file mode 100644 index 0000000..e69de29