Overview
Check out example code and sample projects for the Adrop Flutter SDK.Sample Project
View the complete sample project on GitHub.Flutter Sample Project
Flutter sample app written in Dart
Quick Start Examples
Banner Ad
Copy
import 'package:flutter/material.dart';
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';
class BannerAdExample extends StatefulWidget {
const BannerAdExample({super.key});
@override
State<BannerAdExample> createState() => _BannerAdExampleState();
}
class _BannerAdExampleState extends State<BannerAdExample> {
bool isLoaded = false;
late AdropBannerView bannerView;
@override
void initState() {
super.initState();
bannerView = AdropBannerView(
unitId: 'PUBLIC_TEST_UNIT_ID_320_100',
listener: AdropBannerListener(
onAdReceived: (unitId, metadata) {
debugPrint('Banner ad received: $unitId');
setState(() => isLoaded = true);
},
onAdClicked: (unitId, metadata) {
debugPrint('Banner ad clicked: $unitId');
},
onAdFailedToReceive: (unitId, errorCode) {
debugPrint('Banner ad failed to receive: $errorCode');
},
),
);
bannerView.load();
}
@override
void dispose() {
bannerView.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Banner Ad')),
body: Column(
children: [
Expanded(
child: Center(child: const Text('Main Content')),
),
if (isLoaded)
SizedBox(
width: MediaQuery.of(context).size.width,
height: 80,
child: bannerView,
),
],
),
);
}
}
Native Ad
Copy
import 'package:flutter/material.dart';
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';
class NativeAdExample extends StatefulWidget {
const NativeAdExample({super.key});
@override
State<NativeAdExample> createState() => _NativeAdExampleState();
}
class _NativeAdExampleState extends State<NativeAdExample> {
bool isLoaded = false;
AdropNativeAd? nativeAd;
@override
void initState() {
super.initState();
nativeAd = AdropNativeAd(
unitId: 'PUBLIC_TEST_UNIT_ID_NATIVE',
listener: AdropNativeListener(
onAdReceived: (ad) {
debugPrint('Native ad received: ${ad.creativeId}');
setState(() => isLoaded = true);
},
onAdClicked: (ad) {
debugPrint('Native ad clicked');
},
onAdFailedToReceive: (ad, errorCode) {
debugPrint('Native ad failed to receive: $errorCode');
},
),
);
nativeAd?.load();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Native Ad')),
body: SingleChildScrollView(
child: Column(
children: [
const Padding(
padding: EdgeInsets.all(16),
child: Text('Main Content'),
),
if (isLoaded) _buildNativeAdView(),
],
),
),
);
}
Widget _buildNativeAdView() {
return AdropNativeAdView(
ad: nativeAd,
child: Container(
margin: const EdgeInsets.all(16),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.shade300),
borderRadius: BorderRadius.circular(8),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Profile
Row(
children: [
if (nativeAd?.properties.profile?.displayLogo != null)
ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Image.network(
nativeAd!.properties.profile!.displayLogo!,
width: 24,
height: 24,
),
),
const SizedBox(width: 8),
Text(
nativeAd?.properties.profile?.displayName ?? '',
style: const TextStyle(
fontSize: 12,
color: Colors.grey,
),
),
],
),
const SizedBox(height: 12),
// Headline
if (nativeAd?.properties.headline != null)
Text(
nativeAd!.properties.headline!,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 4),
// Body
if (nativeAd?.properties.body != null)
Text(
nativeAd!.properties.body!,
style: const TextStyle(
fontSize: 14,
color: Colors.grey,
),
),
const SizedBox(height: 12),
// Image
if (nativeAd?.properties.asset != null)
ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
nativeAd!.properties.asset!,
width: double.infinity,
fit: BoxFit.cover,
),
),
const SizedBox(height: 12),
// CTA Button
if (nativeAd?.properties.callToAction != null)
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {},
child: Text(nativeAd!.properties.callToAction!),
),
),
],
),
),
);
}
}
Interstitial Ad
Copy
import 'package:flutter/material.dart';
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';
class InterstitialAdExample extends StatefulWidget {
const InterstitialAdExample({super.key});
@override
State<InterstitialAdExample> createState() => _InterstitialAdExampleState();
}
class _InterstitialAdExampleState extends State<InterstitialAdExample> {
bool isLoaded = false;
AdropInterstitialAd? interstitialAd;
@override
void initState() {
super.initState();
_createInterstitialAd();
}
void _createInterstitialAd() {
interstitialAd?.dispose();
interstitialAd = AdropInterstitialAd(
unitId: 'PUBLIC_TEST_UNIT_ID_INTERSTITIAL',
listener: AdropInterstitialListener(
onAdReceived: (ad) {
debugPrint('Interstitial ad loaded');
setState(() => isLoaded = true);
},
onAdClicked: (ad) {
debugPrint('Interstitial ad clicked');
},
onAdDidPresentFullScreen: (ad) {
debugPrint('Interstitial ad presented');
},
onAdDidDismissFullScreen: (ad) {
debugPrint('Interstitial ad dismissed');
// Load new ad
_createInterstitialAd();
interstitialAd?.load();
},
onAdFailedToReceive: (ad, errorCode) {
debugPrint('Interstitial ad failed to load: $errorCode');
},
),
);
setState(() => isLoaded = false);
}
@override
void dispose() {
interstitialAd?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Interstitial Ad')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => interstitialAd?.load(),
child: const Text('Load Ad'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: isLoaded ? () => interstitialAd?.show() : null,
child: const Text('Show Ad'),
),
],
),
),
);
}
}
Rewarded Ad
Copy
import 'package:flutter/material.dart';
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';
class RewardedAdExample extends StatefulWidget {
const RewardedAdExample({super.key});
@override
State<RewardedAdExample> createState() => _RewardedAdExampleState();
}
class _RewardedAdExampleState extends State<RewardedAdExample> {
bool isLoaded = false;
int coins = 0;
AdropRewardedAd? rewardedAd;
@override
void initState() {
super.initState();
_createRewardedAd();
}
void _createRewardedAd() {
rewardedAd?.dispose();
rewardedAd = AdropRewardedAd(
unitId: 'PUBLIC_TEST_UNIT_ID_REWARDED',
listener: AdropRewardedListener(
onAdReceived: (ad) {
debugPrint('Rewarded ad loaded');
setState(() => isLoaded = true);
},
onAdDidDismissFullScreen: (ad) {
debugPrint('Rewarded ad dismissed');
// Load new ad
_createRewardedAd();
rewardedAd?.load();
},
onAdEarnRewardHandler: (ad, type, amount) {
debugPrint('Reward earned: type=$type, amount=$amount');
setState(() => coins += amount);
_showRewardSnackBar(amount);
},
onAdFailedToReceive: (ad, errorCode) {
debugPrint('Rewarded ad failed to load: $errorCode');
},
),
);
setState(() => isLoaded = false);
}
void _showRewardSnackBar(int amount) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('You earned $amount coins!'),
backgroundColor: Colors.green,
),
);
}
@override
void dispose() {
rewardedAd?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Rewarded Ad')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Coin display
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.amber.shade100,
borderRadius: BorderRadius.circular(8),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.monetization_on, color: Colors.amber),
const SizedBox(width: 8),
Text(
'$coins Coins',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
],
),
),
const SizedBox(height: 32),
ElevatedButton(
onPressed: () => rewardedAd?.load(),
child: const Text('Load Ad'),
),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: isLoaded ? () => rewardedAd?.show() : null,
icon: const Icon(Icons.play_circle_outline),
label: const Text('Watch Ad & Get Reward'),
style: ElevatedButton.styleFrom(
backgroundColor: isLoaded ? Colors.green : Colors.grey,
),
),
],
),
),
);
}
}
Popup Ad
Copy
import 'package:flutter/material.dart';
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';
class PopupAdExample extends StatefulWidget {
const PopupAdExample({super.key});
@override
State<PopupAdExample> createState() => _PopupAdExampleState();
}
class _PopupAdExampleState extends State<PopupAdExample> {
bool isLoaded = false;
AdropPopupAd? popupAd;
@override
void initState() {
super.initState();
_createPopupAd();
}
void _createPopupAd() {
popupAd?.dispose();
popupAd = AdropPopupAd(
unitId: 'PUBLIC_TEST_UNIT_ID_POPUP_BOTTOM',
closeTextColor: Colors.white,
hideForTodayTextColor: Colors.white70,
backgroundColor: Colors.black87,
listener: AdropPopupListener(
onAdReceived: (ad) {
debugPrint('Popup ad loaded');
setState(() => isLoaded = true);
},
onAdDidDismissFullScreen: (ad) {
debugPrint('Popup ad dismissed');
_createPopupAd();
},
onAdFailedToReceive: (ad, errorCode) {
debugPrint('Popup ad failed to load: $errorCode');
if (errorCode == AdropErrorCode.adHideForToday) {
debugPrint('User chose to hide for today.');
}
},
),
);
setState(() => isLoaded = false);
}
@override
void dispose() {
popupAd?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Popup Ad')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => popupAd?.load(),
child: const Text('Load Ad'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: isLoaded ? () => popupAd?.show() : null,
child: const Text('Show Ad'),
),
],
),
),
);
}
}
Advanced Examples
Dark Mode Support
Copy
import 'package:flutter/material.dart';
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_initializeAdrop();
}
Future<void> _initializeAdrop() async {
await Adrop.initialize(true);
// Auto-follow system theme
await Adrop.setTheme(AdropTheme.auto);
}
@override
void didChangePlatformBrightness() {
// Handle system theme changes
// Automatically handled when using AdropTheme.auto
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
themeMode: ThemeMode.system,
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
home: const HomeScreen(),
);
}
}
User Targeting Setup
Copy
import 'package:flutter/material.dart';
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';
class UserProfileScreen extends StatelessWidget {
final User user;
const UserProfileScreen({super.key, required this.user});
Future<void> _setupTargeting() async {
// Set UID
await Adrop.setUID(user.id);
// Set basic properties
await AdropMetrics.setProperty('birth', user.birthDate);
await AdropMetrics.setProperty('gender', user.gender);
// Set custom properties
await AdropMetrics.setProperty('membership_level', user.membershipLevel);
await AdropMetrics.setProperty('total_purchase', user.totalPurchase);
await AdropMetrics.setProperty('is_premium', user.isPremium);
// Log event
await AdropMetrics.logEvent('profile_view', {
'user_id': user.id,
'membership': user.membershipLevel,
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Profile')),
body: Center(
child: ElevatedButton(
onPressed: _setupTargeting,
child: const Text('Setup Targeting'),
),
),
);
}
}
class User {
final String id;
final String birthDate;
final String gender;
final String membershipLevel;
final int totalPurchase;
final bool isPremium;
User({
required this.id,
required this.birthDate,
required this.gender,
required this.membershipLevel,
required this.totalPurchase,
required this.isPremium,
});
}
Ad Preloading
Copy
import 'package:flutter/material.dart';
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';
class AdPreloader {
static AdropInterstitialAd? _interstitialAd;
static AdropRewardedAd? _rewardedAd;
static bool _isInterstitialReady = false;
static bool _isRewardedReady = false;
static void preloadInterstitial() {
_interstitialAd?.dispose();
_interstitialAd = AdropInterstitialAd(
unitId: 'YOUR_INTERSTITIAL_UNIT_ID',
listener: AdropInterstitialListener(
onAdReceived: (ad) {
_isInterstitialReady = true;
debugPrint('Interstitial ad ready');
},
onAdDidDismissFullScreen: (ad) {
_isInterstitialReady = false;
// Reload for next time
Future.delayed(const Duration(seconds: 1), preloadInterstitial);
},
onAdFailedToReceive: (ad, errorCode) {
_isInterstitialReady = false;
// Retry
Future.delayed(const Duration(seconds: 5), preloadInterstitial);
},
),
);
_interstitialAd?.load();
}
static void preloadRewarded() {
_rewardedAd?.dispose();
_rewardedAd = AdropRewardedAd(
unitId: 'YOUR_REWARDED_UNIT_ID',
listener: AdropRewardedListener(
onAdReceived: (ad) {
_isRewardedReady = true;
debugPrint('Rewarded ad ready');
},
onAdDidDismissFullScreen: (ad) {
_isRewardedReady = false;
Future.delayed(const Duration(seconds: 1), preloadRewarded);
},
onAdFailedToReceive: (ad, errorCode) {
_isRewardedReady = false;
Future.delayed(const Duration(seconds: 5), preloadRewarded);
},
),
);
_rewardedAd?.load();
}
static bool get isInterstitialReady => _isInterstitialReady;
static bool get isRewardedReady => _isRewardedReady;
static void showInterstitial() {
if (_isInterstitialReady) {
_interstitialAd?.show();
_isInterstitialReady = false;
}
}
static void showRewarded(
void Function(int type, int amount)? onEarnReward) {
if (_isRewardedReady) {
if (onEarnReward != null) {
_rewardedAd = AdropRewardedAd(
unitId: 'YOUR_REWARDED_UNIT_ID',
listener: AdropRewardedListener(
onAdEarnRewardHandler: (ad, type, amount) {
onEarnReward(type, amount);
},
onAdDidDismissFullScreen: (ad) {
_isRewardedReady = false;
preloadRewarded();
},
),
);
}
_rewardedAd?.show();
_isRewardedReady = false;
}
}
static void dispose() {
_interstitialAd?.dispose();
_rewardedAd?.dispose();
_interstitialAd = null;
_rewardedAd = null;
_isInterstitialReady = false;
_isRewardedReady = false;
}
}
// Call on app start
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Adrop.initialize(true);
// Preload ads
AdPreloader.preloadInterstitial();
AdPreloader.preloadRewarded();
runApp(const MyApp());
}