import 'package:flutter/material.dart';
import 'package:adwhale_sdk_flutter/adwhale_sdk_flutter.dart';
class InterstitialAdScreen extends StatefulWidget {
const InterstitialAdScreen({super.key});
@override
State<InterstitialAdScreen> createState() => _InterstitialAdScreenState();
}
class _InterstitialAdScreenState extends State<InterstitialAdScreen> {
AdWhaleInterstitialAd? _interstitialAd;
bool _isLoaded = false;
@override
void initState() {
super.initState();
_initializeAndLoadInterstitial();
}
Future<void> _initializeAndLoadInterstitial() async {
// SDK 초기화
final result = await AdWhaleMediationAds.instance.initialize();
if (result.isSuccess) {
print('SDK 초기화 성공: ${result.message}');
_loadInterstitial();
} else {
print('SDK 초기화 실패: ${result.statusCode}, ${result.message}');
}
}
void _loadInterstitial() {
_interstitialAd?.destroy();
_interstitialAd = null;
_isLoaded = false;
_interstitialAd = AdWhaleInterstitialAd(
appCode: 'your-placement-uid',
adLoadCallback: AdWhaleInterstitialAdLoadCallback(
onLoaded: () {
print('전면 광고 로드 성공');
setState(() {
_isLoaded = true;
});
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('전면 광고 로드 완료')),
);
}
},
onLoadFailed: (errorCode, errorMessage) {
print('전면 광고 로드 실패: $errorCode, $errorMessage');
setState(() {
_interstitialAd = null;
_isLoaded = false;
});
},
onShowed: () {
print('전면 광고 표시됨');
},
onShowFailed: (errorCode, errorMessage) {
print('전면 광고 표시 실패: $errorCode, $errorMessage');
setState(() {
_interstitialAd = null;
_isLoaded = false;
});
},
onClosed: () {
print('전면 광고 닫힘');
setState(() {
_interstitialAd = null;
_isLoaded = false;
});
// 다음 광고 미리 로드
_loadInterstitial();
},
onClicked: () {
print('전면 광고 클릭됨');
},
),
)
..setRegion('서울시 서초구')
..setGcoder(37.49, 127.02)
..setPlacementName('test_interstitial');
_interstitialAd!.loadAd();
}
void _showInterstitial() {
if (_interstitialAd != null && _isLoaded) {
_interstitialAd!.showAd();
_isLoaded = false;
_interstitialAd = null;
} else {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('전면 광고를 먼저 로드해 주세요.')),
);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('전면 광고 예제')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _loadInterstitial,
child: const Text('광고 로드'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _isLoaded ? _showInterstitial : null,
child: const Text('광고 표시'),
),
],
),
),
);
}
@override
void dispose() {
_interstitialAd?.destroy();
super.dispose();
}
}