diff --git a/samples/admob/app_open_example/lib/app_bar_item.dart b/samples/admob/app_open_example/lib/app_bar_item.dart new file mode 100644 index 000000000..e77175cee --- /dev/null +++ b/samples/admob/app_open_example/lib/app_bar_item.dart @@ -0,0 +1,9 @@ +class AppBarItem { + static const adInpsectorText = 'Ad Inspector'; + static const privacySettingsText = 'Privacy Settings'; + + final String label; + final int value; + + AppBarItem(this.label, this.value); +} diff --git a/samples/admob/app_open_example/lib/main.dart b/samples/admob/app_open_example/lib/main.dart index ece68e33d..ec3fabe5f 100644 --- a/samples/admob/app_open_example/lib/main.dart +++ b/samples/admob/app_open_example/lib/main.dart @@ -16,9 +16,10 @@ import 'package:flutter/material.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart'; + +import 'app_bar_item.dart'; import 'app_lifecycle_reactor.dart'; import 'app_open_ad_manager.dart'; - import 'consent_manager.dart'; void main() { @@ -49,10 +50,9 @@ class HomePage extends StatefulWidget { /// Example home page for an app open ad. class _HomePageState extends State { - static const privacySettingsText = 'Privacy Settings'; - final _appOpenAdManager = AppOpenAdManager(); var _isMobileAdsInitializeCalled = false; + var _isPrivacyOptionsRequired = false; int _counter = 0; late AppLifecycleReactor _appLifecycleReactor; @@ -71,6 +71,9 @@ class _HomePageState extends State { "${consentGatheringError.errorCode}: ${consentGatheringError.message}"); } + // Check if a privacy options entry point is required. + _getIsPrivacyOptionsRequired(); + // Attempt to initialize the Mobile Ads SDK. _initializeMobileAdsSDK(); }); @@ -90,9 +93,7 @@ class _HomePageState extends State { return Scaffold( appBar: AppBar( title: const Text('App Open Demo Home Page'), - actions: _isMobileAdsInitializeCalled - ? _privacySettingsAppBarAction() - : null, + actions: _appBarActions(), ), body: Center( child: Column( @@ -116,38 +117,49 @@ class _HomePageState extends State { ); } - List _privacySettingsAppBarAction() { + List _appBarActions() { + var array = [AppBarItem(AppBarItem.adInpsectorText, 0)]; + + if (_isPrivacyOptionsRequired) { + array.add(AppBarItem(AppBarItem.privacySettingsText, 1)); + } + return [ - // Regenerate the options menu to include a privacy setting. - FutureBuilder( - future: ConsentManager.instance.isPrivacyOptionsRequired(), - builder: (context, snapshot) { - final bool visibility = snapshot.data ?? false; - return Visibility( - visible: visibility, - child: PopupMenuButton( - onSelected: (String result) { - if (result == privacySettingsText) { - ConsentManager.instance - .showPrivacyOptionsForm((formError) { - if (formError != null) { - debugPrint( - "${formError.errorCode}: ${formError.message}"); - } - }); - } - }, - itemBuilder: (BuildContext context) => - >[ - const PopupMenuItem( - value: privacySettingsText, - child: Text(privacySettingsText)) - ], - )); + PopupMenuButton( + itemBuilder: (context) => array + .map((item) => PopupMenuItem( + value: item, + child: Text( + item.label, + ), + )) + .toList(), + onSelected: (item) { + switch (item.value) { + case 0: + MobileAds.instance.openAdInspector((error) { + // Error will be non-null if ad inspector closed due to an error. + }); + case 1: + ConsentManager.instance.showPrivacyOptionsForm((formError) { + if (formError != null) { + debugPrint("${formError.errorCode}: ${formError.message}"); + } + }); + } }) ]; } + /// Redraw the app bar actions if a privacy options entry point is required. + void _getIsPrivacyOptionsRequired() async { + if (await ConsentManager.instance.isPrivacyOptionsRequired()) { + setState(() { + _isPrivacyOptionsRequired = true; + }); + } + } + /// Initialize the Mobile Ads SDK if the SDK has gathered consent aligned with /// the app's configured messages. void _initializeMobileAdsSDK() async { @@ -155,14 +167,12 @@ class _HomePageState extends State { return; } - var canRequestAds = await ConsentManager.instance.canRequestAds(); - if (canRequestAds) { - setState(() { - _isMobileAdsInitializeCalled = true; - }); + if (await ConsentManager.instance.canRequestAds()) { + _isMobileAdsInitializeCalled = true; // Initialize the Mobile Ads SDK. MobileAds.instance.initialize(); + // Load an ad. _appOpenAdManager.loadAd(); }