Created
December 3, 2019 10:45
-
-
Save sagarshende23/242ee73a3f67f038d01fd866064f4a86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:firebase_admob/firebase_admob.dart'; | |
const String testDevice = 'MobileId'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
static const MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo( | |
testDevices: testDevice != null ? <String>[testDevice] : null, | |
nonPersonalizedAds: true, | |
keywords: <String>['Game', 'Mario'], | |
); | |
BannerAd _bannerAd; | |
InterstitialAd _interstitialAd; | |
BannerAd createBannerAd() { | |
return BannerAd( | |
adUnitId: BannerAd.testAdUnitId, | |
//Change BannerAd adUnitId with Admob ID | |
size: AdSize.banner, | |
targetingInfo: targetingInfo, | |
listener: (MobileAdEvent event) { | |
print("BannerAd $event"); | |
}); | |
} | |
InterstitialAd createInterstitialAd() { | |
return InterstitialAd( | |
adUnitId: InterstitialAd.testAdUnitId, | |
//Change Interstitial AdUnitId with Admob ID | |
targetingInfo: targetingInfo, | |
listener: (MobileAdEvent event) { | |
print("IntersttialAd $event"); | |
}); | |
} | |
@override | |
void initState() { | |
FirebaseAdMob.instance.initialize(appId: BannerAd.testAdUnitId); | |
//Change appId With Admob Id | |
_bannerAd = createBannerAd() | |
..load() | |
..show(); | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
_bannerAd.dispose(); | |
_interstitialAd.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
title: 'Flutter Demo', | |
theme: ThemeData(), | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text("Demo App"), | |
), | |
body: Center( | |
child: RaisedButton( | |
child: Text('Click on Ads'), | |
onPressed: () { | |
createInterstitialAd() | |
..load() | |
..show(); | |
}, | |
)), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment