google_mobile_ads 0.12.2+1 google_mobile_ads: ^0.12.2+1 copied to clipboard
Flutter plugin for Google Mobile Ads, supporting banner, interstitial (full-screen), rewarded and native ads
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ignore_for_file: public_member_api_docs
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'reusable_inline_example.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(MyApp());
}
// 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 = 'YOUR_DEVICE_ID';
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static final AdRequest request = AdRequest(
testDevices: <String>[testDevice],
keywords: <String>['foo', 'bar'],
contentUrl: 'http://foo.com/bar.html',
nonPersonalizedAds: true,
);
late InterstitialAd _interstitialAd;
bool _interstitialReady = false;
late RewardedAd _rewardedAd;
bool _rewardedReady = false;
BannerAd? _anchoredBanner;
bool _loadingAnchoredBanner = false;
@override
void initState() {
super.initState();
_createInterstitialAd();
_createRewardedAd();
}
Future<void> _createAnchoredBanner(BuildContext context) async {
final AnchoredAdaptiveBannerAdSize? size =
await AdSize.getAnchoredAdaptiveBannerAdSize(
Orientation.portrait,
MediaQuery.of(context).size.width.truncate(),
);
if (size == null) {
print('Unable to get height of anchored banner.');
return;
}
final BannerAd banner = BannerAd(
size: size,
request: request,
adUnitId: Platform.isAndroid
? 'ca-app-pub-3940256099942544/6300978111'
: 'ca-app-pub-3940256099942544/2934735716',
listener: AdListener(
onAdLoaded: (Ad ad) {
print('$BannerAd loaded.');
setState(() {
_anchoredBanner = ad as BannerAd?;
});
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('$BannerAd failedToLoad: $error');
ad.dispose();
},
onAdOpened: (Ad ad) => print('$BannerAd onAdOpened.'),
onAdClosed: (Ad ad) => print('$BannerAd onAdClosed.'),
onApplicationExit: (Ad ad) => print('$BannerAd onApplicationExit.'),
),
);
return banner.load();
}
void _createInterstitialAd() {
_interstitialAd = InterstitialAd(
adUnitId: InterstitialAd.testAdUnitId,
request: request,
listener: AdListener(
onAdLoaded: (Ad ad) {
print('$InterstitialAd loaded.');
_interstitialReady = true;
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('$InterstitialAd failed to load: $error.');
ad.dispose();
},
onAdOpened: (Ad ad) => print('$InterstitialAd onAdOpened.'),
onAdClosed: (Ad ad) {
print('$InterstitialAd closed.');
ad.dispose();
_createInterstitialAd();
},
onApplicationExit: (Ad ad) =>
print('$InterstitialAd onApplicationExit.'),
),
)..load();
}
void _createRewardedAd() {
_rewardedAd = RewardedAd(
adUnitId: RewardedAd.testAdUnitId,
request: request,
listener: AdListener(
onAdLoaded: (Ad ad) {
print('$RewardedAd loaded.');
_rewardedReady = true;
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('$RewardedAd failed to load: $error');
ad.dispose();
},
onAdOpened: (Ad ad) => print('$RewardedAd onAdOpened.'),
onAdClosed: (Ad ad) {
print('$RewardedAd closed.');
ad.dispose();
_createRewardedAd();
},
onApplicationExit: (Ad ad) => print('$RewardedAd onApplicationExit.'),
onRewardedAdUserEarnedReward: (RewardedAd ad, RewardItem reward) {
print(
'$RewardedAd with reward $RewardItem(${reward.amount}, ${reward.type})',
);
}),
)..load();
}
@override
void dispose() {
super.dispose();
_interstitialAd.dispose();
_rewardedAd.dispose();
_anchoredBanner?.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Builder(builder: (BuildContext context) {
if (!_loadingAnchoredBanner) {
_loadingAnchoredBanner = true;
_createAnchoredBanner(context);
}
return Scaffold(
appBar: AppBar(
title: const Text('AdMob Plugin example app'),
actions: <Widget>[
PopupMenuButton<String>(
onSelected: (String result) {
switch (result) {
case 'InterstitialAd':
if (!_interstitialReady) return;
_interstitialAd.show();
_interstitialReady = false;
break;
case 'RewardedAd':
if (!_rewardedReady) return;
_rewardedAd.show();
_rewardedReady = false;
break;
default:
throw AssertionError('unexpected button: $result');
}
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
PopupMenuItem<String>(
value: '$InterstitialAd',
child: Text('$InterstitialAd'),
),
PopupMenuItem<String>(
value: '$RewardedAd',
child: Text('$RewardedAd'),
),
],
),
],
),
body: SafeArea(
child: Stack(
alignment: AlignmentDirectional.bottomCenter,
children: <Widget>[
ReusableInlineExample(),
if (_anchoredBanner != null)
Container(
color: Colors.green,
width: _anchoredBanner!.size.width.toDouble(),
height: _anchoredBanner!.size.height.toDouble(),
child: AdWidget(ad: _anchoredBanner!),
),
],
),
),
);
}),
);
}
}