Explorar o código

增加applovin max广告集成; release v1.0.1+2

guoziyun hai 6 meses
pai
achega
3540310cae
Modificáronse 5 ficheiros con 68 adicións e 75 borrados
  1. 10 20
      lib/ads/ad_helper.dart
  2. 25 25
      lib/homepage/home_screen.dart
  3. 2 1
      lib/main.dart
  4. 30 28
      lib/play/board_play.dart
  5. 1 1
      pubspec.yaml

+ 10 - 20
lib/ads/ad_helper.dart

@@ -1,48 +1,38 @@
 import 'dart:io';
 
-import 'package:flutter/foundation.dart';
-
-import '../config/config.dart';
-
 class AdHelper {
   ////////////////// applovin //////////////////
 
   // applovin ad sdk 初始化需要的sdkkey
-  static String get applovinSdkKey => 'Re0UgCOtX5ilJER-0B4LpK-5cR4iPs1sU2LAFk1nqlW7zz6-9BJyWI28k6r8yt-RvKc82dwtMQ-KO5IXHcUpvW';
+  static String applovinSdkKey = 'mycRUJHDmgMv3i2NFQ5L5rB_T2y4HBOIIFR7_gpPd3bDCLep4Bb_KfWTsdWTrafiKMQMOyuAGFDOTTmagsk4LM';
 
   static String get applovinBannerAdUnitId {
     if (Platform.isAndroid) {
-      if (kReleaseMode && !Config.isDebug) return '8bc21670e20e8f18';
-      return '8bc21670e20e8f18'; // test id
+      return '0c6a650f98d3832e';
     } else if (Platform.isIOS) {
-      if (kReleaseMode && !Config.isDebug) return '8bc21670e20e8f18';
-      return '8bc21670e20e8f18'; // test id
+      return '';
     } else {
-      return '8bc21670e20e8f18';
+      return '';
     }
   }
 
   static String get applovinInterstitialAdUnitId {
     if (Platform.isAndroid) {
-      if (kReleaseMode && !Config.isDebug) return 'ace63b1d8232da10';
-      return 'ace63b1d8232da10'; // test id
+      return 'd072aec9f9a9b4a4';
     } else if (Platform.isIOS) {
-      if (kReleaseMode && !Config.isDebug) return 'ace63b1d8232da10';
-      return 'ace63b1d8232da10'; // test id
+      return '';
     } else {
-      return 'ace63b1d8232da10';
+      return '';
     }
   }
 
   static String get applovinRewardedAdUnitId {
     if (Platform.isAndroid) {
-      if (kReleaseMode && !Config.isDebug) return '9d218c119bc40d06';
-      return '9d218c119bc40d06'; // test id
+      return '4e4bf93a3a0314d8';
     } else if (Platform.isIOS) {
-      if (kReleaseMode && !Config.isDebug) return '9d218c119bc40d06';
-      return '9d218c119bc40d06'; // test id
+      return '';
     } else {
-      return '9d218c119bc40d06';
+      return '';
     }
   }
 }

+ 25 - 25
lib/homepage/home_screen.dart

@@ -29,6 +29,7 @@ import 'package:puzzleweave/play/board_play.dart';
 import 'package:puzzleweave/settings/settings_screen.dart';
 import 'package:puzzleweave/skin/skin.dart';
 import 'package:puzzleweave/utils/mybutton.dart';
+import '../ads/ads_state.dart';
 
 final Logger _log = Logger('home_screen');
 
@@ -41,7 +42,7 @@ class HomeScreen extends StatefulWidget {
 
 const int minimumRemoteLoadCount = 30; // 假设加载到 30 张图才算网络畅通
 
-class _HomeScreen extends State<HomeScreen> with TickerProviderStateMixin {
+class _HomeScreen extends AdsState<HomeScreen> with TickerProviderStateMixin {
   late Device device;
   late JcAudioController audio;
   late Data data;
@@ -368,25 +369,24 @@ class _HomeScreen extends State<HomeScreen> with TickerProviderStateMixin {
               ],
             ),
           ),
-          Container(),
-          // SafeArea(
-          //   child: SizedBox(
-          //     // 始终预留一个固定的高度,防止布局跳变
-          //     height: context.read<Device>().bannerHeight,
-          //     width: double.infinity,
-          //     child: FutureBuilder<bool>(
-          //       future: _bannerReadyAndShouldShow(),
-          //       builder: (context, snapshot) {
-          //         if (snapshot.hasData && snapshot.data == true) {
-          //           return adBanner;
-          //         }
-          //         return Container(
-          //           // color: Colors.grey.shade100,
-          //         );
-          //       },
-          //     ),
-          //   ),
-          // ),
+          SafeArea(
+            child: SizedBox(
+              // 始终预留一个固定的高度,防止布局跳变
+              height: context.read<Device>().bannerHeight,
+              width: double.infinity,
+              child: FutureBuilder<bool>(
+                future: _bannerReadyAndShouldShow(),
+                builder: (context, snapshot) {
+                  if (snapshot.hasData && snapshot.data == true) {
+                    return adBanner;
+                  }
+                  return Container(
+                    // color: Colors.grey.shade100,
+                  );
+                },
+              ),
+            ),
+          ),
         ],
       ),
     );
@@ -495,7 +495,7 @@ class _HomeScreen extends State<HomeScreen> with TickerProviderStateMixin {
       // await platform.setAdvertiserTrackingEnabled(true);
     }
     initFCM(); // 消息推送许可弹窗
-    // initAd(); // admod 的广告加载安排在iOS ATT 之后,以便能够加载到个性化广告
+    initAd(); // admod 的广告加载安排在iOS ATT 之后,以便能够加载到个性化广告
 
     final idfa = await AppTrackingTransparency.getAdvertisingIdentifier();
     _log.info("idfa: $idfa");
@@ -571,10 +571,10 @@ class _HomeScreen extends State<HomeScreen> with TickerProviderStateMixin {
   }
 
   /// gallery页面加载的时候,可能广告模块还没有初始化完毕
-  // Future<bool> _bannerReadyAndShouldShow() async {
-  //   bool ready = await adSDKReady();
-  //   return ready && shouldShowBannerAd(data.currentLevel);
-  // }
+  Future<bool> _bannerReadyAndShouldShow() async {
+    bool ready = await adSDKReady();
+    return ready && shouldShowBannerAd(data.currentLevel);
+  }
 
   /////////////////////////// FCM ///////////////////////////
   // 消息推送许可弹框

+ 2 - 1
lib/main.dart

@@ -10,6 +10,7 @@ import 'package:flutter/services.dart';
 import 'package:logging/logging.dart';
 import 'package:path_provider/path_provider.dart';
 import 'package:provider/provider.dart';
+import 'package:puzzleweave/ads/applovin_ads_controller.dart';
 import 'package:puzzleweave/app_lifecycle/app_lifecycle.dart';
 import 'package:puzzleweave/audio/jc_audio_controller.dart';
 import 'package:puzzleweave/firebase/firebase_options.dart';
@@ -156,7 +157,7 @@ class MyApp extends StatelessWidget {
             dispose: (context, audio) => audio.dispose(),
           ),
 
-          // Provider<ApplovinAdsController>(create: (context) => ApplovinAdsController(context)),
+          Provider<ApplovinAdsController>(create: (context) => ApplovinAdsController(context)),
           Provider<cfg.Config>(lazy: false, create: (context) => config),
           Provider<Device>(lazy: false, create: (context) => config.device),
         ],

+ 30 - 28
lib/play/board_play.dart

@@ -28,6 +28,7 @@ import 'package:puzzleweave/utils/mybutton.dart';
 import 'package:puzzleweave/utils/utils.dart';
 import 'package:vector_math/vector_math.dart' as vmath;
 import 'package:vibration/vibration.dart';
+import '../ads/ads_state.dart';
 
 final Logger _log = Logger('board_play.dart');
 
@@ -70,7 +71,7 @@ class BoardPlay extends StatefulWidget {
   }
 }
 
-class _BoardPlayState extends State<BoardPlay> with TickerProviderStateMixin {
+class _BoardPlayState extends AdsState<BoardPlay> with TickerProviderStateMixin {
   final GlobalKey boardKey = GlobalKey();
   Board? board;
   bool _isLoading = true;
@@ -1055,10 +1056,10 @@ class _BoardPlayState extends State<BoardPlay> with TickerProviderStateMixin {
   }
 
   /// gallery页面加载的时候,可能广告模块还没有初始化完毕
-  // Future<bool> _bannerReadyAndShouldShow() async {
-  //   bool ready = await adSDKReady();
-  //   return ready && shouldShowBannerAd(data.currentLevel);
-  // }
+  Future<bool> _bannerReadyAndShouldShow() async {
+    bool ready = await adSDKReady();
+    return ready && shouldShowBannerAd(data.currentLevel);
+  }
 
   @override
   Widget build(BuildContext context) {
@@ -1074,25 +1075,24 @@ class _BoardPlayState extends State<BoardPlay> with TickerProviderStateMixin {
             bottom: 0,
             left: 0,
             right: 0,
-            child: Container(),
-            // child: SafeArea(
-            //   child: SizedBox(
-            //     // 始终预留一个固定的高度,防止布局跳变
-            //     height: context.read<Device>().bannerHeight,
-            //     width: double.infinity,
-            //     child: FutureBuilder<bool>(
-            //       future: _bannerReadyAndShouldShow(),
-            //       builder: (context, snapshot) {
-            //         if (snapshot.hasData && snapshot.data == true) {
-            //           return adBanner;
-            //         }
-            //         return Container(
-            //           // color: Colors.grey.shade100,
-            //         );
-            //       },
-            //     ),
-            //   ),
-            // ),
+            child: SafeArea(
+              child: SizedBox(
+                // 始终预留一个固定的高度,防止布局跳变
+                height: context.read<Device>().bannerHeight,
+                width: double.infinity,
+                child: FutureBuilder<bool>(
+                  future: _bannerReadyAndShouldShow(),
+                  builder: (context, snapshot) {
+                    if (snapshot.hasData && snapshot.data == true) {
+                      return adBanner;
+                    }
+                    return Container(
+                      // color: Colors.grey.shade100,
+                    );
+                  },
+                ),
+              ),
+            ),
           ),
           successBanner,
           nextButton,
@@ -1231,10 +1231,12 @@ class _BoardPlayState extends State<BoardPlay> with TickerProviderStateMixin {
         gradient: LinearGradient(colors: [SkinHelper.coreBgColor, SkinHelper.slotBorderColor]),
         onPressed: () async {
           audio.playSfx(SfxType.click);
-          // audio.pauseMusic();
-          // await showInterstitialAd('level_exit', widget.item.id, data.currentLevel);
-          // audio.startMusic();
-          // if (!mounted) return;
+          ///////////////// 播放插屏广告 //////////////////
+          audio.pauseMusic();
+          await showInterstitialAd('level_exit', widget.item.id, data.currentLevel);
+          audio.startMusic();
+          if (!mounted) return;
+          //////////////// 插屏广告结束 /////////////////
           Navigator.pop(context, true);
         },
         child: Text(AppLocalizations.of(context)!.next, style: TextStyle(color: Colors.white, fontSize: 20)),

+ 1 - 1
pubspec.yaml

@@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
 # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
 # In Windows, build-name is used as the major, minor, and patch parts
 # of the product and file versions while build-number is used as the build suffix.
-version: 1.0.0+2
+version: 1.0.1+2
 
 environment:
   sdk: ^3.8.1