Last active
July 27, 2019 17:23
-
-
Save Andrious/3be68118e29210796395eb995c064604 to your computer and use it in GitHub Desktop.
AdMob in Flutter Example
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:ads/ads.dart'; | |
// You can also test with your own ad unit IDs by registering your device as a | |
// test device. Check the logs for your device's ID value. | |
const String testDevice = 'Samsung_Galaxy_SII_API_26:5554'; | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => new _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
int _coins = 0; | |
@override | |
void initState() { | |
super.initState(); | |
Ads.init('ca-app-pub-3940256099942544', testing: true); | |
Ads.video.rewardedListener = (String rewardType, int rewardAmount){ | |
setState(() { | |
_coins += rewardAmount; | |
}); | |
}; | |
Ads.showBannerAd(this); | |
} | |
@override | |
void dispose() { | |
Ads.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
home: new Scaffold( | |
appBar: new AppBar( | |
title: const Text('AdMob Plugin example app'), | |
), | |
body: new SingleChildScrollView( | |
child: new Center( | |
child: new Column( | |
crossAxisAlignment: CrossAxisAlignment.center, | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
new RaisedButton( | |
child: const Text('SHOW BANNER'), | |
onPressed: () { | |
Ads.showBannerAd(this); | |
}), | |
new RaisedButton( | |
child: const Text('REMOVE BANNER'), | |
onPressed: () { | |
Ads.hideBannerAd(); | |
}), | |
new RaisedButton( | |
child: const Text('SHOW INTERSTITIAL'), | |
onPressed: () { | |
Ads.showFullScreenAd(this); | |
}, | |
), | |
new RaisedButton( | |
child: const Text('SHOW REWARDED VIDEO'), | |
onPressed: () { | |
Ads.showVideoAd(this); | |
}, | |
), | |
new Text("You have $_coins coins."), | |
].map((Widget button) { | |
return new Padding( | |
padding: const EdgeInsets.symmetric(vertical: 16.0), | |
child: button, | |
); | |
}).toList(), | |
), | |
), | |
), | |
), | |
); | |
} | |
} | |
void main() { | |
runApp(new MyApp()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment