import 'package:flutter/material.dart';
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';
class PopupAdPage extends StatefulWidget {
const PopupAdPage({super.key});
@override
State<PopupAdPage> createState() => _PopupAdPageState();
}
class _PopupAdPageState extends State<PopupAdPage> {
bool isLoaded = false;
bool isLoading = false;
bool isShown = false;
AdropErrorCode? errorCode;
AdropPopupAd? popupAd;
@override
void initState() {
super.initState();
_createPopupAd();
}
void _createPopupAd() {
popupAd?.dispose();
popupAd = AdropPopupAd(
unitId: 'PUBLIC_TEST_UNIT_ID_POPUP_BOTTOM', // テストユニットID
closeTextColor: Colors.white,
hideForTodayTextColor: Colors.white70,
backgroundColor: Colors.black87,
listener: AdropPopupListener(
onAdReceived: (ad) {
debugPrint('広告ロード成功');
debugPrint('クリエイティブID: ${ad.creativeId}');
debugPrint('キャンペーンID: ${ad.campaignId}');
setState(() {
isLoaded = true;
isLoading = false;
errorCode = null;
});
},
onAdClicked: (ad) {
debugPrint('広告クリック: ${ad.destinationURL}');
},
onAdImpression: (ad) {
debugPrint('広告表示: ${ad.creativeId}');
},
onAdWillPresentFullScreen: (ad) {
debugPrint('広告表示予定');
},
onAdDidPresentFullScreen: (ad) {
debugPrint('広告が表示されました');
},
onAdWillDismissFullScreen: (ad) {
debugPrint('広告が閉じられる予定');
},
onAdDidDismissFullScreen: (ad) {
debugPrint('広告が閉じられました');
setState(() {
isShown = true;
});
},
onAdFailedToReceive: (ad, error) {
debugPrint('広告ロード失敗: $error');
setState(() {
isLoaded = false;
isLoading = false;
errorCode = error;
});
},
onAdFailedToShowFullScreen: (ad, error) {
debugPrint('広告表示失敗: $error');
setState(() {
errorCode = error;
});
},
),
);
setState(() {
isLoaded = false;
isShown = false;
errorCode = null;
});
}
void loadAd() {
setState(() {
isLoading = true;
errorCode = null;
});
popupAd?.load();
}
void showAd() {
if (isLoaded) {
popupAd?.show();
}
}
@override
void dispose() {
popupAd?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('ポップアップ広告の例'),
),
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// ロードボタン
ElevatedButton(
onPressed: isLoading ? null : loadAd,
child: Text(isLoading ? 'ロード中...' : '広告をロード'),
),
const SizedBox(height: 16),
// 表示ボタン
ElevatedButton(
onPressed: isLoaded ? showAd : null,
child: const Text('広告を表示'),
),
const SizedBox(height: 16),
// リセットボタン
TextButton(
onPressed: (isShown || errorCode != null)
? () => _createPopupAd()
: null,
child: const Text('リセット'),
),
const SizedBox(height: 24),
// ステータス表示
Text('ロード済み: ${isLoaded ? "はい" : "いいえ"}'),
Text('表示済み: ${isShown ? "はい" : "いいえ"}'),
// エラー表示
if (errorCode != null) ...[
const SizedBox(height: 16),
Text(
'エラー: ${errorCode?.code}',
style: const TextStyle(color: Colors.red),
),
if (errorCode == AdropErrorCode.adHideForToday)
const Text(
'(ユーザーが今日は表示しないを選択しました)',
style: TextStyle(color: Colors.grey, fontSize: 12),
),
],
],
),
),
),
);
}
}