76 lines
1.6 KiB
Dart
76 lines
1.6 KiB
Dart
import "package:flutter/foundation.dart" show kIsWeb;
|
|
import "package:flutter/widgets.dart";
|
|
import "package:shadcn_flutter/shadcn_flutter.dart";
|
|
import "../services/ad_service.dart";
|
|
import "../services/admob_service.dart";
|
|
import "../services/adsense_service.dart";
|
|
|
|
class AdBanner extends StatefulWidget {
|
|
const AdBanner({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<AdBanner> createState() => _AdBannerState();
|
|
}
|
|
|
|
class _AdBannerState extends State<AdBanner> {
|
|
late AdService _adService;
|
|
bool _isLoading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initializeAds();
|
|
}
|
|
|
|
Future<void> _initializeAds() async {
|
|
// Platform detection
|
|
if (kIsWeb) {
|
|
_adService = AdSenseService();
|
|
} else {
|
|
_adService = AdMobService();
|
|
}
|
|
|
|
try {
|
|
await _adService.initialize();
|
|
await _adService.loadBannerAd();
|
|
} catch (e) {
|
|
print("Error initializng ads: $e");
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_adService.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_isLoading) {
|
|
return SizedBox(
|
|
height: 50,
|
|
child: Center(
|
|
child: SizedBox(
|
|
width: 20,
|
|
height: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
if (!_adService.isLoaded) {
|
|
// Ad failed to load, show nothing
|
|
return SizedBox.shrink();
|
|
}
|
|
|
|
return _adService.getBannerWidget();
|
|
}
|
|
}
|