gdpr_ads 0.1.0+1 gdpr_ads: ^0.1.0+1 copied to clipboard
GDPR service, GDPR intermediate screen, Admob manager for loading and displaying
GDPR service, GDPR intermediate screen, Admob manager for loading and displaying
Features #
- Drop-in GDPR widgets suitable for for App startup.
- Ad loading and displaying.
- In Flutter Web GDPR and Ads fail gracefully.
Getting started #
This package consists of two components: GDPR and Ads.
Both parts work in conjunction, but can also be used individually, more details in the GDPR and Ads README.
Below is an example combining both parts.
The steps:
- Optional: Update AndroidManifest.xml with Admob App ID.
- Set
home: GdprCheckWrapper()
. - Copy
GdprCheckWrapper.dart
and paste your IDs into it. - Display Banner Ads or Intertestial Ads in your code.
Initialize GDPR and Ads #
void main() async {
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: GdprCheckWrapper(),
);
}
}
Update existing GDPR #
Scaffold(
body:
GdprCheckWrapper(showUpdateConsentForm: true),
);
Create a Helper Widget: GdprCheckWrapper #
Copy the helper widget below, it will either show the inital or update GDPR dialog.
Paste your Admob Ad IDs and Test Device ID into it.
This way your GDPR and Ad related configuration stays in one widget only.
import 'package:example_flutter/aftergdprcheck.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:gdpr_ads/ads/adservice.dart';
import 'package:gdpr_ads/gdpr/gdprpage.dart';
import 'package:gdpr_ads/gdpr/gdprservice.dart';
import 'package:gdpr_ads/gdpr/gdprupdatepage.dart';
class GdprCheckWrapper extends StatelessWidget {
final bool resetConfirmationForm;
final bool showUpdateConsentForm;
const GdprCheckWrapper(
{this.resetConfirmationForm = false,
this.showUpdateConsentForm = false,
Key? key})
: super(key: key);
void _initAdmob() {
AdService().initialize(
bannerIds: ['ca-app-pub-3940256099942544/6300978111'],
interstitialIds: ['ca-app-pub-3940256099942544/1033173712'],
interRewardIds: ['ca-app-pub-3940256099942544/5354046379'],
testDeviceIds: [' ... YOUR PHONE DEVICE ID ... ']);
}
@override
Widget build(BuildContext context) {
return showUpdateConsentForm == false
?
// ---------------------------------------------------------------
// SHOWS AN INITIAL CONSENT FORM
//
// IT WILL ONLY SHOW IF NO CONSENT HAS BEEN GIVEN PRIOR
// ---------------------------------------------------------------
GdprPage(
() async => _initAdmob(), // no await
() {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => const AfterGdprPage()),
);
},
debugTestIdentifiers: [' ... YOUR PHONE DEVICE ID ... '],
showDebugUI: kDebugMode,
debugResetConsentForm: resetConfirmationForm,
loadingWidget: const Center(child:
CircularProgressIndicator()),
debugGeography: GdprDebugGeography.disabled,
)
:
// ---------------------------------------------------------------
// SHOWS AN UPDATE CONSENT FORM
//
// IT WILL ALLWAYS SHOW
// ---------------------------------------------------------------
GdprUpdatePage(
() async => _initAdmob(), // no await
() {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => const AfterGdprPage()));
},
debugTestIdentifiers: [' ... YOUR PHONE DEVICE ID ... '],
showDebugUI: kDebugMode,
loadingWidget: const Center(child:
CircularProgressIndicator()),
);
}
}
Show Banner Ads #
If no adUnitId
is given the first available Ad unit will be displayed.
// In [kDebugMode] some debug info will be shown
// before the Ad is loaded or in case of error.
Scaffold(
body:
AdBanner(String? adUnitId),
);
Show Interstitial Ads and get the result #
If no adUnitId
is given the first available Ad unit will be displayed.
final ResponseInterstitial result
= await AdService().showInterstitial(String? adUnitId);
// Or call:
final ResponseInterstitialRewarded result
= await AdService().showInterstitialRewarded(
context, String? adUnitId);
// The return values look like this:
ResponseInterstitialRewarded {
StatusInterstitialRewarded status;
num? rewardAmount;
String? rewardType;
int? admobErrorCode;
String? admobErrorMessage;
}
enum StatusInterstitialRewarded {
notLoadedOnWeb,
notLoadedGenerally,
notLoadedAdIdNotSet,
displaySuccess,
displayDeniedByUser,
displayDeniedProgrammatically,
}
Example #
The example folder contains a working example. Set your test device ID before running it, the Ad units can stay as they are. Refrain from hitting the Interstitial buttons too quickly in a row, it will trigger a new background ad fetch at the moment.
TODO #
- Bounce request for Interstitial and InterstitialRewarded ads when these are still loading.
Additional information #
Google Admob can be painful, Ad slots may not get filled by Admob sometimes, also not sure if VPNs play well with Admob.
Admob project configuration