ad helper
This commit is contained in:
38
covas_mobile/lib/classes/ad_helper.dart
Normal file
38
covas_mobile/lib/classes/ad_helper.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
|
||||
class AdHelper {
|
||||
static BannerAd? bannerAd;
|
||||
static bool isBannerAdLoaded = false;
|
||||
|
||||
static Future<void> loadBannerAd(Function setStateCallback) async {
|
||||
await dotenv.load(fileName: ".env");
|
||||
final adUnitId = dotenv.env['AD_UNIT_ID'] ?? '';
|
||||
|
||||
bannerAd = BannerAd(
|
||||
adUnitId: adUnitId, // Replace with actual ID
|
||||
size: AdSize.banner,
|
||||
request: AdRequest(),
|
||||
listener: BannerAdListener(
|
||||
onAdLoaded: (ad) {
|
||||
setStateCallback(() {
|
||||
isBannerAdLoaded = true;
|
||||
});
|
||||
},
|
||||
onAdFailedToLoad: (ad, error) {
|
||||
print('Banner Ad failed to load: $error');
|
||||
isBannerAdLoaded = false;
|
||||
ad.dispose();
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
bannerAd?.load();
|
||||
}
|
||||
|
||||
static void disposeAd() {
|
||||
bannerAd?.dispose();
|
||||
bannerAd = null;
|
||||
isBannerAdLoaded = false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user