Last active
December 8, 2018 20:58
-
-
Save Andrious/1fe8c218f4fe0f689df31d221b7298ba to your computer and use it in GitHub Desktop.
Main Dart file demonstrating the ads.dart implementation for AdMobs in Flutter
This file contains hidden or 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
| // Copyright 2017 The Chromium Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| import 'package:flutter/material.dart'; | |
| import 'package:ads/ads.dart'; | |
| import 'package:firebase_admob/firebase_admob.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(); | |
| var initOption = 1; | |
| switch (initOption) { | |
| case 1: | |
| Ads.init('ca-app-pub-3940256099942544', testing: true); | |
| Ads.showBannerAd(this); | |
| Ads.video.rewardedListener = (String rewardType, int rewardAmount){ | |
| setState(() { | |
| _coins += rewardAmount; | |
| }); | |
| }; | |
| break; | |
| case 2: | |
| var eventListener = (MobileAdEvent event){ | |
| if(event == MobileAdEvent.closed) | |
| print("Returned to the app."); | |
| }; | |
| Ads.init( | |
| 'ca-app-pub-3940256099942544', | |
| keywords: <String>['ibm', 'computers'], | |
| contentUrl: 'http://www.ibm.com', | |
| birthday: DateTime.utc(1989, 11, 9), | |
| gender: MobileAdGender.female, | |
| designedForFamilies: true, | |
| childDirected: false, | |
| testDevices: ['Samsung_Galaxy_SII_API_26:5554'], | |
| testing: false, | |
| listener: eventListener, | |
| ); | |
| Ads.showBannerAd(this); | |
| break; | |
| case 3: | |
| Ads.init('ca-app-pub-3940256099942544', testing: true); | |
| // You can set individual settings | |
| Ads.keywords = ['cats', 'dogs']; | |
| Ads.contentUrl = 'http://www.animalsaspets.com'; | |
| Ads.birthday = DateTime.utc(1967, 11, 02); | |
| Ads.gender = MobileAdGender.unknown; | |
| Ads.designedForFamilies = false; | |
| Ads.childDirected = false; | |
| Ads.testDevices = ['Samsung_Galaxy_SII_API_26:5554']; | |
| Ads.testing = true; | |
| Ads.bannerListener = (MobileAdEvent event){ | |
| if(event == MobileAdEvent.opened){ | |
| print("This is the first listener."); | |
| } | |
| }; | |
| Ads.showBannerAd(this); | |
| break; | |
| case 4: | |
| Ads.init('ca-app-pub-3940256099942544'); | |
| var eventListener = (MobileAdEvent event){ | |
| if(event == MobileAdEvent.closed) | |
| print("Returned to the app."); | |
| }; | |
| /// You can set the Banner, Fullscreen and Video Ads separately. | |
| Ads.setBannerAd( | |
| size: AdSize.banner, | |
| keywords: ['andriod, flutter'], | |
| contentUrl: 'http://www.andrioussolutions.com', | |
| birthday: DateTime.utc(1977,12,31), | |
| gender: MobileAdGender.unknown, | |
| designedForFamilies: true, | |
| childDirected: false, | |
| testDevices: ['Samsung_Galaxy_SII_API_26:5554'], | |
| listener: eventListener, | |
| ); | |
| Ads.setFullScreenAd( | |
| keywords: ['dart', 'flutter'], | |
| contentUrl: 'http://www.fluttertogo.com', | |
| birthday: DateTime.utc(19,86, 12, 15), | |
| gender: MobileAdGender.male, | |
| designedForFamilies: true, | |
| childDirected: false, | |
| testDevices: ['Samsung_Galaxy_SII_API_26:5554'], | |
| listener: (MobileAdEvent event){ | |
| if(event == MobileAdEvent.opened) | |
| print("Opened the Ad."); | |
| } | |
| ); | |
| var videoListener = (RewardedVideoAdEvent event, {String rewardType, int rewardAmount}){ | |
| if(event == RewardedVideoAdEvent.closed){ | |
| print("Returned to the app."); | |
| } | |
| }; | |
| Ads.setVideoAd( | |
| keywords: ['dart', 'java'], | |
| contentUrl: 'http://www.publang.org', | |
| birthday: DateTime.utc(1991, 12,01), | |
| gender: MobileAdGender.female, | |
| designedForFamilies: false, | |
| childDirected: false, | |
| testDevices: null, | |
| listener: videoListener, | |
| ); | |
| break; | |
| case 5: | |
| Ads.init('ca-app-pub-3940256099942544'); | |
| /// You can set individual settings | |
| Ads.keywords = ['cats', 'dogs']; | |
| Ads.contentUrl = 'http://www.animalsaspets.com'; | |
| Ads.birthday = DateTime.utc(1967, 11, 02); | |
| Ads.gender = MobileAdGender.unknown; | |
| Ads.designedForFamilies = false; | |
| Ads.childDirected = false; | |
| Ads.testDevices = ['Samsung_Galaxy_SII_API_26:5554']; | |
| /// Can set this at the init() function instead. | |
| Ads.testing = true; | |
| break; | |
| case 6: | |
| Ads.init('ca-app-pub-3940256099942544', testing: true); | |
| Ads.eventListener = (MobileAdEvent event){ | |
| switch (event){ | |
| case MobileAdEvent.loaded: | |
| print("The Ad is loading into memory. Not necessarily displayed yet."); | |
| break; | |
| case MobileAdEvent.failedToLoad: | |
| print("The Ad failed to load into memory."); | |
| break; | |
| case MobileAdEvent.clicked: | |
| print("The Ad has been clicked and opened."); | |
| break; | |
| case MobileAdEvent.impression: | |
| print("The displayed Ad has 'changed' to a new one."); | |
| break; | |
| case MobileAdEvent.opened: | |
| print("The Ad is now open."); | |
| break; | |
| case MobileAdEvent.leftApplication: | |
| print("You've left the app after clicking the Ad."); | |
| break; | |
| case MobileAdEvent.closed: | |
| print("You've closed the Ad and returned to the app."); | |
| break; | |
| default: | |
| print("There's a 'new' MobileAdEvent?!"); | |
| } | |
| }; | |
| Ads.showBannerAd(this); | |
| break; | |
| case 7: | |
| Ads.init('ca-app-pub-3940256099942544', testing: true); | |
| Ads.showBannerAd(this); | |
| Ads.bannerListener = (MobileAdEvent event){ | |
| if(event == MobileAdEvent.opened){ | |
| print("You've clicked on the Banner Ad"); | |
| } | |
| }; | |
| Ads.screenListener = (MobileAdEvent event){ | |
| if(event == MobileAdEvent.opened){ | |
| print("You've clicked on the Fullscreen Ad."); | |
| } | |
| }; | |
| Ads.videoListener = (RewardedVideoAdEvent event, {String rewardType, int rewardAmount}){ | |
| if(event == RewardedVideoAdEvent.opened){ | |
| print("You've clicked on the Fullscreen Ad."); | |
| } | |
| }; | |
| break; | |
| case 8: | |
| Ads.init('ca-app-pub-3940256099942544', testing: true); | |
| Ads.showBannerAd(this); | |
| Ads.banner.openedListener = (){ | |
| print("This is the first listener when you open the banner ad."); | |
| }; | |
| Ads.banner.openedListener = (){ | |
| print("This is the second listener when you open the banner ad."); | |
| }; | |
| Ads.banner.leftAppListener = (){ | |
| print("You've left your app."); | |
| }; | |
| Ads.banner.closedListener = (){ | |
| print("You've closed an ad and returned to your app."); | |
| }; | |
| break; | |
| case 9: | |
| Ads.init('ca-app-pub-3940256099942544', testing: true); | |
| Ads.showBannerAd(this); | |
| Ads.screen.openedListener = (){ | |
| print("This is the first listener when you open the full screen ad."); | |
| }; | |
| Ads.screen.openedListener = (){ | |
| print("This is the second listener when you open the full screen ad."); | |
| }; | |
| Ads.screen.leftAppListener = (){ | |
| print("You've left your app."); | |
| }; | |
| Ads.screen.closedListener = (){ | |
| print("You've closed an ad and returned to your app."); | |
| }; | |
| break; | |
| case 10: | |
| Ads.init('ca-app-pub-3940256099942544', testing: true); | |
| Ads.showBannerAd(this); | |
| Ads.video.closedListener = (){ | |
| print("You've closed the video."); | |
| }; | |
| Ads.video.rewardedListener = (String rewardType, int rewardAmount){ | |
| setState(() { | |
| _coins += rewardAmount; | |
| }); | |
| }; | |
| break; | |
| default: | |
| Ads.init('ca-app-pub-3940256099942544', testing: true); | |
| Ads.showBannerAd(this); | |
| } | |
| var one = Ads.appId; | |
| var two = Ads.keywords; | |
| var three = Ads.contentUrl; | |
| var four = Ads.birthday; | |
| var five = Ads.gender; | |
| var six = Ads.designedForFamilies; | |
| var seven = Ads.childDirected; | |
| var eight = Ads.testDevices; | |
| var nine = Ads.testing; | |
| } | |
| @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