Skip to content

Commit

Permalink
Add ad inspector to native template example (#1131)
Browse files Browse the repository at this point in the history
* Add ad inspector button

* ioS 12

* formatted

---------

Co-authored-by: Justin Malandruccolo <[email protected]>
  • Loading branch information
malandr2 and Justin Malandruccolo authored Jul 16, 2024
1 parent afa237d commit 6da4b59
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@
DEVELOPMENT_TEAM = EQHXZ8M8AV;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -507,6 +508,7 @@
DEVELOPMENT_TEAM = EQHXZ8M8AV;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -530,6 +532,7 @@
DEVELOPMENT_TEAM = EQHXZ8M8AV;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
9 changes: 9 additions & 0 deletions samples/admob/native_template_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/native_template_example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'app_bar_item.dart';
import 'consent_manager.dart';

void main() {
Expand All @@ -12,7 +13,7 @@ void main() {
));
}

/// A simple app that loads a native ad.
/// An example app that loads a native ad.
class NativeExample extends StatefulWidget {
const NativeExample({super.key});

Expand All @@ -21,10 +22,9 @@ class NativeExample extends StatefulWidget {
}

class NativeExampleState extends State<NativeExample> {
static const privacySettingsText = 'Privacy Settings';

final _consentManager = ConsentManager();
var _isMobileAdsInitializeCalled = false;
var _isPrivacyOptionsRequired = false;
NativeAd? _nativeAd;
bool _nativeAdIsLoaded = false;

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

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

// Attempt to initialize the Mobile Ads SDK.
_initializeMobileAdsSDK();
});
Expand All @@ -60,10 +63,7 @@ class NativeExampleState extends State<NativeExample> {
title: 'Native Example',
home: Scaffold(
appBar: AppBar(
title: const Text('Native Example'),
actions: _isMobileAdsInitializeCalled
? _privacySettingsAppBarAction()
: null),
title: const Text('Native Example'), actions: _appBarActions()),
body: SizedBox(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
Expand Down Expand Up @@ -96,33 +96,36 @@ class NativeExampleState extends State<NativeExample> {
)));
}

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.isPrivacyOptionsRequired(),
builder: (context, snapshot) {
final bool visibility = snapshot.data ?? false;
return Visibility(
visible: visibility,
child: PopupMenuButton<String>(
onSelected: (String result) {
if (result == privacySettingsText) {
_consentManager.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.showPrivacyOptionsForm((formError) {
if (formError != null) {
debugPrint("${formError.errorCode}: ${formError.message}");
}
});
}
})
];
}
Expand Down Expand Up @@ -185,21 +188,28 @@ class NativeExampleState extends State<NativeExample> {
..load();
}

/// Redraw the app bar actions if a privacy options entry point is required.
void _getIsPrivacyOptionsRequired() async {
if (await _consentManager.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.canRequestAds();
if (canRequestAds) {
setState(() {
_isMobileAdsInitializeCalled = true;
});
if (await _consentManager.canRequestAds()) {
_isMobileAdsInitializeCalled = true;

// Initialize the Mobile Ads SDK.
MobileAds.instance.initialize();

// Load an ad.
_loadAd();
}
Expand Down
2 changes: 1 addition & 1 deletion samples/admob/native_template_example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
sdk: '>=2.18.0 <3.0.0'
sdk: '>=3.2.0 <4.0.0'

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
Expand Down

0 comments on commit 6da4b59

Please sign in to comment.