adalam_flutter 0.1.1
adalam_flutter: ^0.1.1 copied to clipboard
AdAlam Ad SDK for Flutter — Banner, Interstitial, and Rewarded ads for Android.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:adalam_flutter/adalam_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
try {
await AdAlam.setTestMode(true);
await AdAlam.initialize(appId: 'app_12345', apiKey: 'apikey_67890');
} on AdAlamException catch (e) {
debugPrint('AdAlam Init Error: $e');
} catch (e) {
debugPrint('Unexpected Error: $e');
}
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final rewardedAd = RewardedAd(placementKey: 'rewarded_main');
final interstitialAd = InterstitialAd(placementKey: 'interstitial_level');
String statusText = 'Ready';
@override
void initState() {
super.initState();
rewardedAd.listener = AdListener(
onLoaded: () => setState(() => statusText = 'Rewarded Loaded'),
onFailed: (e) => setState(() => statusText = 'Rewarded Failed: $e'),
onReward: (r) =>
setState(() => statusText = 'Reward: ${r.amount} ${r.type}'),
onClosed: () => setState(() => statusText = 'Rewarded Closed'),
onImpression: () => debugPrint('Rewarded Impression'),
onClicked: () => debugPrint('Rewarded Clicked'),
);
interstitialAd.listener = AdListener(
onLoaded: () => setState(() => statusText = 'Interstitial Loaded'),
onFailed: (e) => setState(() => statusText = 'Interstitial Failed: $e'),
onClosed: () => setState(() => statusText = 'Interstitial Closed'),
onImpression: () => debugPrint('Interstitial Impression'),
onClicked: () => debugPrint('Interstitial Clicked'),
);
}
@override
void dispose() {
rewardedAd.dispose();
interstitialAd.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('AdAlam Flutter Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
statusText,
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () => rewardedAd.load(),
child: const Text('Load Rewarded'),
),
ElevatedButton(
onPressed: () async {
if (rewardedAd.isLoaded) {
await rewardedAd.show();
} else {
setState(() => statusText = 'Ad not loaded yet');
}
},
child: const Text('Show Rewarded'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () => interstitialAd.load(),
child: const Text('Load Interstitial'),
),
ElevatedButton(
onPressed: () async {
if (interstitialAd.isLoaded) {
await interstitialAd.show();
} else {
setState(() => statusText = 'Ad not loaded yet');
}
},
child: const Text('Show Interstitial'),
),
const SizedBox(height: 20),
BannerAdWidget(
placementKey: 'home_banner',
size: BannerSize.standard,
listener: AdListener(
onLoaded: () => debugPrint('Banner Loaded'),
onFailed: (e) => debugPrint('Banner Failed: $e'),
onClicked: () => debugPrint('Banner Clicked'),
),
),
],
),
),
),
);
}
}