adquanta_ads_sdk 1.7.2
adquanta_ads_sdk: ^1.7.2 copied to clipboard
AdQuanta Ads SDK for Flutter - 专业的移动广告聚合SDK
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:adquanta_ads_sdk/adquanta_ads_sdk.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _status = 'SDK is not initialized';
bool _initialized = false;
final AdquantaNativeController _nativeController = AdquantaNativeController();
late final AdquantaAdListener _nativeListener = _ExampleNativeListener(
onStatusChanged: _updateStatus,
);
late final AdquantaAdListener _gameListener = _ExampleGameListener(
onStatusChanged: _updateStatus,
);
void _updateStatus(String status) {
if (!mounted) return;
setState(() {
_status = status;
});
}
Future<void> _initializeSdk() async {
if (!AdquantaSdk.isSupported) {
_updateStatus('AdQuanta ads are supported on Android and iOS only.');
return;
}
try {
final result = await AdquantaSdk.init(
appId: 'YOUR_APP_ID',
gameEnabled: true,
);
if (!mounted) return;
setState(() {
_status = result.initialized
? 'SDK initialized'
: 'SDK initialization failed';
_initialized = result.initialized;
});
} catch (error) {
if (!mounted) return;
setState(() {
_status = 'SDK initialization failed: $error';
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Plugin example app')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Column(
children: [
Text(_status, textAlign: TextAlign.center),
const SizedBox(height: 16),
ElevatedButton(
onPressed: AdquantaSdk.isSupported ? _initializeSdk : null,
child: const Text('Initialize SDK'),
),
if (!AdquantaSdk.isSupported) ...[
const SizedBox(height: 16),
const Text(
'AdQuanta ads are supported on Android and iOS only.',
),
],
if (AdquantaSdk.isSupported && _initialized) ...[
const SizedBox(height: 24),
AdquantaNative(
key: const ValueKey('feed_native'),
adUnitId: 'feed_native',
height: 320,
controller: _nativeController,
listener: _nativeListener,
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () => _nativeController.loadAd(),
child: const Text('Reload Native'),
),
const SizedBox(height: 24),
AdquantaGame(
key: const ValueKey('game_center_tab'),
adUnitId: 'game_center_tab',
androidDrawableName: 'game_center_entry',
height: 96,
listener: _gameListener,
),
],
],
),
),
),
);
}
}
class _ExampleNativeListener extends AdquantaAdListener {
_ExampleNativeListener({required this.onStatusChanged});
final ValueChanged<String> onStatusChanged;
@override
void onAdLoaded(AdquantaAdInfo adInfo) {
onStatusChanged('Native loaded (${adInfo.provider})');
}
@override
void onAdLoadFailed(AdquantaAdError error) {
onStatusChanged('Native failed: ${error.message}');
}
@override
void onAdShowFailed(AdquantaAdError error, AdquantaAdInfo adInfo) {
onStatusChanged('Native render failed: ${error.message}');
}
@override
void onAdClicked(AdquantaAdInfo adInfo) {
onStatusChanged('Native clicked');
}
@override
void onAdImpression(AdquantaAdInfo adInfo) {
onStatusChanged('Native impression');
}
}
class _ExampleGameListener extends AdquantaAdListener {
_ExampleGameListener({required this.onStatusChanged});
final ValueChanged<String> onStatusChanged;
@override
void onAdLoaded(AdquantaAdInfo adInfo) {
onStatusChanged('Game Center loaded (${adInfo.provider})');
}
@override
void onAdLoadFailed(AdquantaAdError error) {
onStatusChanged('Game Center failed: ${error.message}');
}
@override
void onAdClicked(AdquantaAdInfo adInfo) {
onStatusChanged('Game Center clicked');
}
}