avory_ad_control 1.0.2
avory_ad_control: ^1.0.2 copied to clipboard
Avory Ad Control is a wrapper around Google Admob which let you integrate ads easily
example/lib/main.dart
import 'dart:async';
import 'dart:io';
import 'package:avory_ad_control/avory_ad_control.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'native_ad_example.dart';
import 'native_platform_example.dart';
const IAdIdManager adIdManager = TestAdIdManager();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await ConsentManager.gatherGdprConsent(
debugGeography: kDebugMode ? DebugGeography.debugGeographyEea : null,
);
await ConsentManager.gatherPrivacyConsent();
// Initialize with AppLovin integration using configurable mediation rates
await AivoryAdControl.instance.initialize(
isShowAppOpenOnAppStateChange: false,
adIdManager,
adMobAdRequest: const AdRequest(),
admobConfiguration: RequestConfiguration(testDeviceIds: []),
showAdBadge: Platform.isIOS,
// Configure mediation rates for different ad types
mediationConfig: const AdMediationConfig(
appOpenRate: 0.7, // 70% AppLovin for app open ads
interstitialRate: 0.7, // 70% AppLovin for interstitial ads
bannerRate: 0.5, // 50% AppLovin for banner ads
),
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Ad Control Example',
home: CountryListScreen(),
);
}
}
class CountryListScreen extends StatefulWidget {
const CountryListScreen({super.key});
@override
State<CountryListScreen> createState() => _CountryListScreenState();
}
class _CountryListScreenState extends State<CountryListScreen> {
/// Using it to cancel the subscribed callbacks
StreamSubscription? _streamSubscription;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Ad Control Example - AdMob + AppLovin"),
centerTitle: true,
),
body: Center(
child: SingleChildScrollView(
child: Column(
children: [
Text(
'App Open Ads',
style: Theme.of(context).textTheme.headlineMedium!.copyWith(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
const Text(
'70% AppLovin / 30% AdMob Distribution (Configurable)',
style: TextStyle(fontSize: 14, color: Colors.grey),
),
AdButton(
networkName: 'AdMob App Open',
onTap: () => _showAd(AdNetwork.admob, AdUnitType.appOpen),
),
AdButton(
networkName: 'AppLovin App Open',
onTap: () => _showAd(AdNetwork.applovin, AdUnitType.appOpen),
),
AdButton(
networkName: 'Smart App Open (Mediated)',
onTap: () => _showAvailableAd(AdUnitType.appOpen),
isHighlighted: true,
),
const Divider(thickness: 2),
Text(
'Interstitial Ads',
style: Theme.of(context).textTheme.headlineMedium!.copyWith(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
const Text(
'70% AppLovin / 30% AdMob Distribution (Configurable)',
style: TextStyle(fontSize: 14, color: Colors.grey),
),
AdButton(
networkName: 'AdMob Interstitial',
onTap: () => _showAd(AdNetwork.admob, AdUnitType.interstitial),
),
AdButton(
networkName: 'AppLovin Interstitial',
onTap:
() => _showAd(AdNetwork.applovin, AdUnitType.interstitial),
),
AdButton(
networkName: 'Smart Interstitial (Mediated)',
onTap: () => _showAvailableAd(AdUnitType.interstitial),
isHighlighted: true,
),
const Divider(thickness: 2),
Text(
'Rewarded Ads',
style: Theme.of(context).textTheme.headlineMedium!.copyWith(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
AdButton(
networkName: 'AdMob Rewarded',
onTap: () => _showAd(AdNetwork.admob, AdUnitType.rewarded),
),
AdButton(
networkName: 'Available Rewarded',
onTap: () => _showAvailableAd(AdUnitType.rewarded),
),
const Divider(thickness: 2),
Text(
'Banner Ads (Demo)',
style: Theme.of(context).textTheme.headlineMedium!.copyWith(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
const Text(
'Banner ads are shown in the detail screen after clicking any ad button above.',
style: TextStyle(fontSize: 14, fontStyle: FontStyle.italic),
),
const SizedBox(height: 8),
const ACBannerAd(
adNetwork: AdNetwork.admob,
adSize: AdSize.banner,
),
const SizedBox(height: 8),
const ACBannerAd(
adNetwork: AdNetwork.applovin,
adFormat: AdFormat.banner,
isAdaptiveBannerEnabled: true,
isAutoRefreshEnabled: true,
),
const Divider(thickness: 2),
Text(
'Native Ad Examples',
style: Theme.of(context).textTheme.headlineMedium!.copyWith(
color: Colors.green,
fontWeight: FontWeight.bold,
),
),
AdButton(
networkName: 'Regular Native Ads',
onTap:
() => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const NativeAdExample(),
),
),
),
AdButton(
networkName: 'Native Platform Example',
onTap:
() => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const NativePlatformExample(),
),
),
),
const SizedBox(height: 20),
Container(
padding: const EdgeInsets.all(16),
margin: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.blue.shade50,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Colors.blue.shade200),
),
child: const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'💡 Revenue Optimization Tips:',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
color: Colors.blue,
),
),
SizedBox(height: 8),
Text(
'• Use "Smart" ads for best revenue (automatic mediation)\n'
'• AppLovin typically provides 20-40% higher eCPMs\n'
'• Intelligent fallback ensures maximum fill rates\n'
'• Both networks preload ads for instant display\n'
'• Banner ads support both AdMob and AppLovin\n'
'• Interstitial ads now support dual network mediation\n'
'• NEW: Fullscreen native ads with custom theming',
style: TextStyle(fontSize: 14),
),
],
),
),
],
),
),
),
);
}
void _showAd(AdNetwork adNetwork, AdUnitType adUnitType) {
if (AivoryAdControl.instance.showAd(
adUnitType,
adNetwork: adNetwork,
context: context,
loaderDuration: 1,
)) {
// Canceling the last callback subscribed
_streamSubscription?.cancel();
// Listening to the callback from showRewardedAd()
_streamSubscription = AivoryAdControl.instance.onEvent.listen((event) {
if (event.adUnitType == adUnitType) {
_streamSubscription?.cancel();
goToNextScreen(adNetwork: adNetwork);
}
});
} else {
goToNextScreen(adNetwork: adNetwork);
}
}
void _showAvailableAd(AdUnitType adUnitType) {
if (AivoryAdControl.instance.showAd(adUnitType)) {
// Canceling the last callback subscribed
_streamSubscription?.cancel();
// Listening to the callback from showRewardedAd()
_streamSubscription = AivoryAdControl.instance.onEvent.listen((event) {
if (event.adUnitType == adUnitType) {
_streamSubscription?.cancel();
goToNextScreen();
}
});
} else {
goToNextScreen();
}
}
void goToNextScreen({AdNetwork? adNetwork}) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CountryDetailScreen(adNetwork: adNetwork),
),
);
}
}
class CountryDetailScreen extends StatefulWidget {
final AdNetwork? adNetwork;
const CountryDetailScreen({super.key, this.adNetwork});
@override
State<CountryDetailScreen> createState() => _CountryDetailScreenState();
}
class _CountryDetailScreenState extends State<CountryDetailScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('United States'), centerTitle: true),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: 200,
decoration: const BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://cdn.britannica.com/33/4833-050-F6E415FE/Flag-United-States-of-America.jpg',
),
),
),
),
widget.adNetwork == null
? Container() // Placeholder for banner ad
: widget.adNetwork == AdNetwork.applovin
? ACBannerAd(
adNetwork: AdNetwork.applovin,
adFormat: AdFormat.banner,
isAdaptiveBannerEnabled: true,
isAutoRefreshEnabled: true,
)
: ACBannerAd(
adNetwork: AdNetwork.admob,
adSize: AdSize.largeBanner,
),
const Expanded(
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(20.0),
child: Text(
'The U.S. is a country of 50 states covering a vast swath of North America, with Alaska in the northwest and Hawaii extending the nation\'s presence into the Pacific Ocean. Major Atlantic Coast cities are New York, a global finance and culture center, and capital Washington, DC. Midwestern metropolis Chicago is known for influential architecture and on the west coast, Los Angeles\' Hollywood is famed for filmmaking',
style: TextStyle(fontWeight: FontWeight.w600, fontSize: 22),
),
),
),
),
],
),
);
}
}
class AdButton extends StatelessWidget {
final String networkName;
final VoidCallback onTap;
final bool isHighlighted;
const AdButton({
super.key,
required this.onTap,
required this.networkName,
this.isHighlighted = false,
});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Card(
color: isHighlighted ? Colors.blue.shade100 : null,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
networkName,
style: const TextStyle(fontSize: 28, fontWeight: FontWeight.w300),
),
),
),
);
}
}