Skip to content

Commit

Permalink
Added ad inspector button
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Malandruccolo committed Jul 9, 2024
1 parent ad4f3b2 commit a2722d0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
9 changes: 9 additions & 0 deletions samples/admob/app_open_example/lib/app_bar_item.dart
Original file line number Diff line number Diff line change
@@ -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);
}
86 changes: 48 additions & 38 deletions samples/admob/app_open_example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -49,10 +50,9 @@ class HomePage extends StatefulWidget {

/// Example home page for an app open ad.
class _HomePageState extends State<HomePage> {
static const privacySettingsText = 'Privacy Settings';

final _appOpenAdManager = AppOpenAdManager();
var _isMobileAdsInitializeCalled = false;
var _isPrivacyOptionsRequired = false;
int _counter = 0;
late AppLifecycleReactor _appLifecycleReactor;

Expand All @@ -71,6 +71,9 @@ class _HomePageState extends State<HomePage> {
"${consentGatheringError.errorCode}: ${consentGatheringError.message}");
}

// Check if a privacy options entry point is required.
_getIsPrivacyOptionsRequired();

// Attempt to initialize the Mobile Ads SDK.
_initializeMobileAdsSDK();
});
Expand All @@ -90,9 +93,7 @@ class _HomePageState extends State<HomePage> {
return Scaffold(
appBar: AppBar(
title: const Text('App Open Demo Home Page'),
actions: _isMobileAdsInitializeCalled
? _privacySettingsAppBarAction()
: null,
actions: _appBarActions(),
),
body: Center(
child: Column(
Expand All @@ -116,53 +117,62 @@ class _HomePageState extends State<HomePage> {
);
}

List<Widget> _privacySettingsAppBarAction() {
List<Widget> _appBarActions() {
var array = [AppBarItem(AppBarItem.adInpsectorText, 0)];

if (_isPrivacyOptionsRequired) {
array.add(AppBarItem(AppBarItem.privacySettingsText, 1));
}

return <Widget>[
// 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<String>(
onSelected: (String result) {
if (result == privacySettingsText) {
ConsentManager.instance
.showPrivacyOptionsForm((formError) {
if (formError != null) {
debugPrint(
"${formError.errorCode}: ${formError.message}");
}
});
}
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<String>>[
const PopupMenuItem<String>(
value: privacySettingsText,
child: Text(privacySettingsText))
],
));
PopupMenuButton<AppBarItem>(
itemBuilder: (context) => array
.map((item) => PopupMenuItem<AppBarItem>(
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 {
if (_isMobileAdsInitializeCalled) {
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();
}
Expand Down

0 comments on commit a2722d0

Please sign in to comment.