110 lines
2.5 KiB
Dart
110 lines
2.5 KiB
Dart
import "dart:io" show Platform;
|
|
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;
|
|
bool _isMacOS = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
// check if running on macOS
|
|
if (!kIsWeb && Platform.isMacOS) {
|
|
_isMacOS = true;
|
|
_isLoading = false;
|
|
} else {
|
|
_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() {
|
|
if (!_isMacOS) {
|
|
_adService.dispose();
|
|
}
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
|
|
// Check if we're on mobile (iOS/Android)
|
|
bool isMobile = !kIsWeb && !_isMacOS && (Platform.isIOS || Platform.isAndroid);
|
|
|
|
// Show placeholder only on macOS and web when ads fail to load
|
|
// On mobile, we want real ads or nothing
|
|
if (_isMacOS || (!isMobile && !_isLoading && !_adService.isLoaded)) {
|
|
return ConstrainedBox(
|
|
constraints: const BoxConstraints(
|
|
minHeight: 80,
|
|
),
|
|
child: OutlinedContainer(
|
|
child: Center(
|
|
child: Text(
|
|
"ADVERTISEMENT SPACE",
|
|
style: TextStyle(
|
|
color: Theme.of(context).colorScheme.mutedForeground,
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
).withPadding(
|
|
horizontal: 8
|
|
);
|
|
}
|
|
|
|
if (_isLoading) {
|
|
return SizedBox(
|
|
height: 50,
|
|
child: Center(
|
|
child: SizedBox(
|
|
width: 20,
|
|
height: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
return _adService.getBannerWidget();
|
|
}
|
|
}
|