Add Google Ads in android Flutter App




Add Google Ads in android Flutter App

 
1- Add this packages to Pubspec.yaml
#google ads
provider: ^6.0.0
google_mobile_ads: ^0.13.4
2- add this to Manifest.xml with your app id
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-4324766906825930~1694408478"/>
(don't forget pub get)
 
3- create adsManger.dart class (just copy and edit it )

import 'dart:io';
import 'package:google_mobile_ads/google_mobile_ads.dart';

class AdsManager {
bool _testMode = true;
Future<InitializationStatus> initialization;
AdsManager({required this.initialization});


String get appId {
if (Platform.isAndroid) {
return "ca-app-pub-4324766906825930~1694408478";
} else {
throw UnsupportedError("Unsupported platform");
}
}

String get bannerAdUnitId {
if (_testMode == true) {
return "ca-app-pub-3940256099942544/6300978111"; //test bannerAdUnitId
} else if (Platform.isAndroid) {
return "ca-app-pub-3940256099942544/6300978111";
} else {
throw UnsupportedError("Unsupported platform");
}
}

BannerAdListener get adListener => _adListener;

final BannerAdListener _adListener = BannerAdListener(
onAdLoaded: (Ad ad) => print('Ad loaded.'),
onAdFailedToLoad: (Ad ad, LoadAdError error) {
ad.dispose();
print('Ad failed to load: $error');
},
// Called when an ad opens an overlay that covers the screen.
onAdOpened: (Ad ad) => print('Ad opened.'),
// Called when an ad removes an overlay that covers the screen.
onAdClosed: (Ad ad) => print('Ad closed.'),
// Called when an impression occurs on the ad.
onAdImpression: (Ad ad) => print('Ad impression.'),
);
}
4- Add this in main() ... and provide adManger variable as you like, i use Provier package.. use what you like :)
void main() {
//------- AdMobAds -----------
WidgetsFlutterBinding.ensureInitialized();
final adInitialize = MobileAds.instance.initialize();
final adManger = AdsManager(initialization: adInitialize);

runApp(Provider.value(
value: adManger,
child: MyApp()));
}
5- finally add this in your page :)
   ///------- AdMobAds ----------- ///
BannerAd? _bottomBannerAd;
bool isLoaded = false;
late AdsManager adManager;

void getBottomBannerAd(AdsManager adManager) {
_bottomBannerAd = BannerAd(
adUnitId: adManager.bannerAdUnitId,
size: AdSize.largeBanner,
request: AdRequest(),
listener: BannerAdListener(
onAdLoaded: (_) {
setState(() {
isLoaded = true;
print('Ad loaded to load: $isLoaded !!!!!!');
});
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
//ad.dispose();
print('Ad failed to load: $error');
},
),
)..load();
}

Widget showAd() {
return isLoaded
? Container(
decoration: BoxDecoration(
border: Border.all(color: Color(0xffffda11), ),
//color: Color(0xffffda11),
),
alignment: Alignment.center,
height: _bottomBannerAd!.size.height.toDouble() ,
width: _bottomBannerAd!.size.width.toDouble(),
child: AdWidget(ad: _bottomBannerAd!),
)
: Container(
color: Colors.black87,
alignment: Alignment.center,
width: double.infinity,
height: 50,
);
}
//------- AdMobAds -----------

@override
void didChangeDependencies() {
super.didChangeDependencies();
//------- AdMobAds -----------
adManager = Provider.of<AdsManager>(context);
adManager.initialization.then((value) {
getBottomBannerAd(adManager);
});
//------- AdMobAds -----------
}

@override
void dispose() {
super.dispose();
_bottomBannerAd!.dispose(); //------- AdMobAds -----------
}
///------- AdMobAds ----------- /// 


6- use  showAd()  anywhere in the tree :)




⭐⭐⭐  Buy Food for my Dog 🐶 ⭐⭐⭐

https://buymeacoffee.com/ramy.dmk


Tags

#buttons=(Accept !) #days=(14)

Our website uses cookies to enhance your experience. Learn More
Accept !