Add Google Ads in android Flutter App
1- Add this packages to Pubspec.yaml
#google ads2- add this to Manifest.xml with your app id
provider: ^6.0.0
google_mobile_ads: ^0.13.4
<meta-data(don't forget pub get)
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-4324766906825930~1694408478"/>
3- create adsManger.dart class (just copy and edit it )
4- Add this in main() ... and provide adManger variable as you like, i use Provier package.. use what you like :)
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.'),
);
}
void main() {5- finally add this in your page :)
//------- AdMobAds -----------
WidgetsFlutterBinding.ensureInitialized();
final adInitialize = MobileAds.instance.initialize();
final adManger = AdsManager(initialization: adInitialize);
runApp(Provider.value(
value: adManger,
child: MyApp()));
}
///------- 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 :)