Prepare Google Play release with age-gated ads and store assets

This commit is contained in:
Hadrian Burkhardt
2026-11-15 18:54:43 +01:00
parent d73c5fc89a
commit 57b8bc20b8
49 changed files with 836 additions and 82 deletions
+23
View File
@@ -0,0 +1,23 @@
/// Broad age groups used only to choose advertising settings.
///
/// Only this choice is stored locally; no birth date is collected. Players who
/// choose [under13] or [unknown] play without advertising, and the ad SDK is
/// never initialized for them.
enum AdAudience {
unknown,
under13,
teen,
adult;
/// The only age information the app persists.
static const storageKey = 'ad_age_group_v1';
bool get allowsAds => this == teen || this == adult;
/// Teens, under-13s and unknown-age players never receive standard adult
/// advertising treatment.
bool get restricted => this != adult;
static AdAudience fromStored(String value) =>
values.where((group) => group.name == value).firstOrNull ?? unknown;
}
+77 -18
View File
@@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'ad_pacing.dart';
import 'ad_audience.dart';
import 'play_metrics.dart';
class Ads {
@@ -13,6 +14,26 @@ class Ads {
final PlayMetrics? metrics;
bool _showing = false;
bool foreground = true;
AdAudience _audience = AdAudience.unknown;
bool _consentReady = false;
int _generation = 0;
AdAudience get audience => _audience;
bool get adsAllowed => supported && _audience.allowsAds;
Future<void> setAudience(AdAudience audience) async {
_audience = audience;
await start();
}
void _clearAds() {
_reward?.dispose();
_interstitial?.dispose();
_reward = null;
_interstitial = null;
_rewardAt = _interstitialAt = null;
_rewardLoading = _interstitialLoading = false;
}
void _paid(AdWithoutView ad, String format) {
ad.onPaidEvent = (ad, value, precision, currency) {
@@ -42,42 +63,61 @@ class Ads {
(defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS);
Future<void> start() async {
if (!supported) return;
// Until neutral age screening is implemented, treat all users as children
// and configure the SDK before consent or ad initialization.
final generation = ++_generation;
_consentReady = false;
_clearAds();
// Under-13 and unknown-age players never initialize the SDK or request ads.
if (!supported || _disposed || !_audience.allowsAds) return;
// Teenagers receive child-directed treatment; adults receive standard
// treatment. Both keep the general-audience rating.
await MobileAds.instance.updateRequestConfiguration(
RequestConfiguration(
ageRestrictedTreatment: AgeRestrictedTreatment.child,
ageRestrictedTreatment: _audience.restricted
? AgeRestrictedTreatment.child
: AgeRestrictedTreatment.unspecified,
maxAdContentRating: MaxAdContentRating.g,
),
);
if (_disposed) return;
if (_disposed || generation != _generation) return;
ConsentInformation.instance.requestConsentInfoUpdate(
ConsentRequestParameters(tagForUnderAgeOfConsent: true),
ConsentRequestParameters(tagForUnderAgeOfConsent: _audience.restricted),
() {
ConsentForm.loadAndShowConsentFormIfRequired((_) => _initialize());
if (_disposed || generation != _generation) return;
ConsentForm.loadAndShowConsentFormIfRequired(
(_) => _initialize(generation),
);
},
(_) => _initialize(),
(_) => _initialize(generation),
);
}
Future<void> _initialize() async {
Future<void> _initialize(int generation) async {
if (_disposed ||
_started ||
generation != _generation ||
!_audience.allowsAds ||
!await ConsentInformation.instance.canRequestAds()) {
return;
}
_started = true;
await MobileAds.instance.initialize();
if (_disposed || generation != _generation) return;
if (!_started) {
_started = true;
await MobileAds.instance.initialize();
}
if (_disposed || generation != _generation) return;
_consentReady = true;
await _load();
}
Future<void> _load() async {
if (_disposed ||
!_consentReady ||
!_audience.allowsAds ||
!supported ||
!await ConsentInformation.instance.canRequestAds()) {
return;
}
final generation = _generation;
if (_disposed || !_consentReady) return;
final ios = defaultTargetPlatform == TargetPlatform.iOS;
if (_reward == null && !_rewardLoading) {
_rewardLoading = true;
@@ -90,8 +130,12 @@ class Ads {
request: const AdRequest(),
rewardedAdLoadCallback: RewardedAdLoadCallback(
onAdLoaded: (a) {
if (generation != _generation) {
a.dispose();
return;
}
_rewardLoading = false;
if (_disposed) {
if (_disposed || !_consentReady) {
a.dispose();
return;
}
@@ -100,6 +144,7 @@ class Ads {
_rewardAt = DateTime.now();
},
onAdFailedToLoad: (_) {
if (generation != _generation) return;
_rewardLoading = false;
},
),
@@ -117,8 +162,12 @@ class Ads {
request: const AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (a) {
if (generation != _generation) {
a.dispose();
return;
}
_interstitialLoading = false;
if (_disposed) {
if (_disposed || !_consentReady) {
a.dispose();
return;
}
@@ -127,6 +176,7 @@ class Ads {
_interstitialAt = DateTime.now();
},
onAdFailedToLoad: (_) {
if (generation != _generation) return;
_interstitialLoading = false;
},
),
@@ -136,6 +186,8 @@ class Ads {
Future<bool> hint(VoidCallback earned) async {
if (!supported ||
!_consentReady ||
!_audience.allowsAds ||
_disposed ||
_showing ||
!foreground ||
@@ -190,6 +242,8 @@ class Ads {
Future<void> between(VoidCallback next) async {
if (!supported ||
!_consentReady ||
!_audience.allowsAds ||
_disposed ||
_showing ||
!foreground ||
@@ -239,19 +293,24 @@ class Ads {
}
Future<bool> privacy() async {
if (!supported) return false;
// No ads means no ad-privacy form and no reason to touch the SDK.
if (!supported || !_audience.allowsAds) return false;
if (await ConsentInformation.instance
.getPrivacyOptionsRequirementStatus() !=
PrivacyOptionsRequirementStatus.required) {
return false;
}
ConsentForm.showPrivacyOptionsForm((_) {});
final generation = ++_generation;
_consentReady = false;
_clearAds();
ConsentForm.showPrivacyOptionsForm((_) => _initialize(generation));
return true;
}
void dispose() {
_disposed = true;
_reward?.dispose();
_interstitial?.dispose();
_generation++;
_consentReady = false;
_clearAds();
}
}