thinkingit_fb_ads 0.3.0
thinkingit_fb_ads: ^0.3.0 copied to clipboard
Meta (Facebook) Audience Network ads for Flutter. Banner, native, native banner, interstitial and rewarded video ads on Android and iOS.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:thinkingit_fb_ads/thinkingit_fb_ads.dart';
// Test placement IDs from Meta. Replace them with your own placement IDs.
const _bannerPlacementId =
'IMG_16_9_APP_INSTALL#2312433698835503_2964944860251047';
const _nativePlacementId =
'IMG_16_9_APP_INSTALL#2312433698835503_2964952163583650';
const _nativeBannerPlacementId =
'IMG_16_9_APP_INSTALL#2312433698835503_2964953543583512';
const _interstitialPlacementId =
'IMG_16_9_APP_INSTALL#2312433698835503_2650502525028617';
const _rewardedPlacementId = 'YOUR_PLACEMENT_ID';
void main() {
WidgetsFlutterBinding.ensureInitialized();
// Add your device's testing ID (printed in the device log) to get test ads.
FbAds.init();
runApp(const AdExampleApp());
}
class AdExampleApp extends StatelessWidget {
const AdExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'thinkingit_fb_ads example',
theme: ThemeData(colorSchemeSeed: Colors.blue),
home: Scaffold(
appBar: AppBar(title: const Text('thinkingit_fb_ads example')),
body: const AdsPage(),
),
);
}
}
class AdsPage extends StatefulWidget {
const AdsPage({super.key});
@override
State<AdsPage> createState() => _AdsPageState();
}
class _AdsPageState extends State<AdsPage> {
bool _isInterstitialAdLoaded = false;
bool _isRewardedAdLoaded = false;
Widget _currentAd = const SizedBox.shrink();
@override
void initState() {
super.initState();
_loadInterstitialAd();
_loadRewardedVideoAd();
}
@override
void dispose() {
FbInterstitialAd.destroyInterstitialAd();
FbRewardedVideoAd.destroyRewardedVideoAd();
super.dispose();
}
void _loadInterstitialAd() {
FbInterstitialAd.loadInterstitialAd(
placementId: _interstitialPlacementId,
listener: (result, value) {
debugPrint('Interstitial ad: $result --> $value');
if (result == FbInterstitialAdResult.loaded) {
_isInterstitialAdLoaded = true;
}
// An interstitial can only be shown once; load a new one after it
// is dismissed.
if (result == FbInterstitialAdResult.dismissed) {
_isInterstitialAdLoaded = false;
_loadInterstitialAd();
}
},
);
}
void _loadRewardedVideoAd() {
FbRewardedVideoAd.loadRewardedVideoAd(
placementId: _rewardedPlacementId,
listener: (result, value) {
debugPrint('Rewarded ad: $result --> $value');
if (result == FbRewardedVideoAdResult.loaded) {
_isRewardedAdLoaded = true;
}
if (result == FbRewardedVideoAdResult.videoComplete) {
debugPrint('Grant the reward here');
}
if (result == FbRewardedVideoAdResult.videoClosed) {
_isRewardedAdLoaded = false;
_loadRewardedVideoAd();
}
},
);
}
void _showInterstitialAd() {
if (_isInterstitialAdLoaded) {
FbInterstitialAd.showInterstitialAd();
} else {
debugPrint('Interstitial ad not loaded yet');
}
}
void _showRewardedAd() {
if (_isRewardedAdLoaded) {
FbRewardedVideoAd.showRewardedVideoAd();
} else {
debugPrint('Rewarded ad not loaded yet');
}
}
void _show(Widget ad) => setState(() => _currentAd = ad);
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16),
child: Wrap(
spacing: 8,
runSpacing: 8,
alignment: WrapAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () => _show(
FbBannerAd(
placementId: _bannerPlacementId,
listener: (result, value) =>
debugPrint('Banner ad: $result --> $value'),
),
),
child: const Text('Banner'),
),
ElevatedButton(
onPressed: () => _show(
FbNativeAd(
placementId: _nativePlacementId,
adType: FbNativeAdType.nativeAd,
height: 300,
backgroundColor: Colors.blue,
titleColor: Colors.white,
descriptionColor: Colors.white,
buttonColor: Colors.deepPurple,
buttonTitleColor: Colors.white,
buttonBorderColor: Colors.white,
listener: (result, value) =>
debugPrint('Native ad: $result --> $value'),
),
),
child: const Text('Native'),
),
ElevatedButton(
onPressed: () => _show(
FbNativeAd(
placementId: _nativeBannerPlacementId,
adType: FbNativeAdType.nativeBannerAd,
bannerAdSize: FbNativeBannerAdSize.height100,
listener: (result, value) =>
debugPrint('Native banner ad: $result --> $value'),
),
),
child: const Text('Native banner'),
),
ElevatedButton(
onPressed: _showInterstitialAd,
child: const Text('Interstitial'),
),
ElevatedButton(
onPressed: _showRewardedAd,
child: const Text('Rewarded'),
),
],
),
),
Expanded(child: Center(child: _currentAd)),
],
);
}
}