pubspec.yaml
dependencies:
flutter:
sdk: flutter
google_mobile_ads: ^3.0.0
ios\Runner\Info.plist
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-6360703854329045~6028133xxx</string>
<key>SKAdNetworkItems</key>
<array>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>cstr6suwn9.skadnetwork</string>
</dict>
....
</array>
main.dart
//AdMobの初期化処理
void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(const MainApp());
}
class _MyHomePageState extends State<MyHomePage> {
final BannerAd myBanner = BannerAd(
adUnitId: AD_UNIT_ID,
size: AdSize.banner,
request: AdRequest(),
listener: BannerAdListener(),
);
final BannerAdListener listener = BannerAdListener(
// Called when an ad is successfully received.
onAdLoaded: (Ad ad) => debugPrint('Ad loaded.'),
// Called when an ad request failed.
onAdFailedToLoad: (Ad ad, LoadAdError error) {
// Dispose the ad here to free resources.
ad.dispose();
debugPrint('Ad failed to load: $error');
},
// Called when an ad opens an overlay that covers the screen.
onAdOpened: (Ad ad) => debugPrint('Ad opened.'),
// Called when an ad removes an overlay that covers the screen.
onAdClosed: (Ad ad) => debugPrint('Ad closed.'),
// Called when an impression occurs on the ad.
onAdImpression: (Ad ad) => debugPrint('Ad impression.'),
);
@override
void initState() {
super.initState();
myBanner.load();
}
@override
Widget build(BuildContext context) {
final AdWidget adWidget = AdWidget(ad: myBanner);
final Container adContainer = Container(
alignment: Alignment.center,
width: myBanner.size.width.toDouble(),
height: myBanner.size.height.toDouble(),
child: adWidget,
);
return Scaffold(
body: Center(
child: Wrap(direction: Axis.horizontal, children: <Widget>[
adContainer,
]),
),
コメント