ray_sigmob_ads 0.1.0
ray_sigmob_ads: ^0.1.0 copied to clipboard
Sigmob ads for Flutter with rewarded, interstitial, and splash formats on Android and iOS.
example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:ray_sigmob_ads/flutter_sigmob.dart';
void main() => runApp(const SigmobExampleApp());
class SigmobExampleApp extends StatelessWidget {
const SigmobExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'flutter_sigmob example',
theme: ThemeData(colorSchemeSeed: Colors.indigo),
home: const SigmobHomePage(),
);
}
}
class SigmobHomePage extends StatefulWidget {
const SigmobHomePage({super.key});
@override
State<SigmobHomePage> createState() => _SigmobHomePageState();
}
class _SigmobHomePageState extends State<SigmobHomePage> {
final _appIdController = TextEditingController(text: '6877');
final _appKeyController = TextEditingController(text: 'eccdcdbd9adbd4a7');
final _rewardedPlacementController = TextEditingController(
text: 'ea1f8f7b662',
);
final _interstitialPlacementController = TextEditingController(
text: 'f51c9186cb9',
);
final _splashPlacementController = TextEditingController(text: 'ea1f8f9bd12');
StreamSubscription<SigmobAdEvent>? _eventSubscription;
SigmobRewardedAd? _rewardedAd;
SigmobInterstitialAd? _interstitialAd;
SigmobSplashAd? _splashAd;
String _status = '请输入 Sigmob App ID 和 App Key';
bool _busy = false;
@override
void initState() {
super.initState();
_eventSubscription = FlutterSigmob.events.listen((event) {
debugPrint(
'flutter_sigmob event: type=${event.type.name}, '
'adType=${event.adType.name}, placementId=${event.placementId}, '
'data=${event.data}, error=${event.error?.code}:'
'${event.error?.message}',
);
if (mounted) {
setState(() => _status = _describeEvent(event));
}
});
}
String _describeEvent(SigmobAdEvent event) {
final adName = switch (event.adType) {
SigmobAdType.rewarded => '激励视频',
SigmobAdType.interstitial => '新插屏',
SigmobAdType.splash => '开屏广告',
_ => '广告',
};
switch (event.type) {
case SigmobAdEventType.serverResponse:
return event.data['isFillAd'] == true
? '$adName数据返回:有填充,正在下载素材'
: '$adName数据返回:无填充,请稍后重试或检查广告位';
case SigmobAdEventType.loaded:
return '$adName加载完成,可以展示';
case SigmobAdEventType.loadFailed:
case SigmobAdEventType.showFailed:
final error = event.error;
return '${event.type.name}:${error?.code ?? 'unknown'} '
'${error?.message ?? ''}';
case SigmobAdEventType.rewarded:
return event.data['isRewarded'] == true ? '激励条件已满足' : '未满足激励条件';
case SigmobAdEventType.closed:
return '$adName已关闭';
default:
return '事件:${event.type.name}';
}
}
Future<void> _run(Future<void> Function() operation) async {
setState(() => _busy = true);
try {
await operation();
} on SigmobException catch (error) {
if (mounted) {
setState(() => _status = '${error.code}: ${error.message}');
}
} on ArgumentError catch (error) {
if (mounted) {
setState(() => _status = error.message?.toString() ?? error.toString());
}
} finally {
if (mounted) setState(() => _busy = false);
}
}
Future<void> _initialize() => _run(() async {
await FlutterSigmob.initialize(
SigmobConfig(
appId: _appIdController.text,
appKey: _appKeyController.text,
debug: true,
privacy: const SigmobPrivacyConfig(
allowLocation: false,
allowOaid: false,
allowAndroidId: false,
allowAppList: false,
allowSimOperator: false,
allowIdfa: true,
allowIdfv: false,
allowMotion: false,
allowDiskSpace: false,
personalizedAdvertising: true,
programmaticAdvertising: true,
),
),
);
if (mounted) setState(() => _status = 'SDK 配置/初始化成功');
});
Future<void> _start() => _run(() async {
await FlutterSigmob.start();
if (mounted) setState(() => _status = 'SDK 启动成功');
});
Future<void> _readSdkVersion() => _run(() async {
final version = await FlutterSigmob.sdkVersion;
if (mounted) setState(() => _status = 'Sigmob SDK $version');
});
Future<void> _loadRewardedAd() => _run(() async {
await _rewardedAd?.dispose();
final ad = SigmobRewardedAd(
request: SigmobAdRequest(
placementId: _rewardedPlacementController.text,
userId: 'flutter-demo-user',
options: const <String, String>{'source': 'flutter_sigmob_example'},
),
);
_rewardedAd = ad;
await ad.load();
if (mounted) setState(() => _status = '激励视频加载请求已提交');
});
Future<void> _showRewardedAd() => _run(() async {
final ad = _rewardedAd;
if (ad == null) {
setState(() => _status = '请先加载激励视频');
return;
}
if (!await ad.isReady) {
setState(() => _status = '激励视频尚未 Ready');
return;
}
await ad.show(sceneId: 'flutter-demo', sceneDescription: 'Flutter 激励视频示例');
});
Future<void> _loadInterstitialAd() => _run(() async {
await _interstitialAd?.dispose();
final ad = SigmobInterstitialAd(
request: SigmobAdRequest(
placementId: _interstitialPlacementController.text,
userId: 'flutter-demo-user',
),
);
_interstitialAd = ad;
await ad.load();
if (mounted) setState(() => _status = '新插屏加载请求已提交');
});
Future<void> _showInterstitialAd() => _run(() async {
final ad = _interstitialAd;
if (ad == null) {
setState(() => _status = '请先加载新插屏广告');
return;
}
if (!await ad.isReady) {
setState(() => _status = '新插屏广告尚未 Ready');
return;
}
await ad.show(
sceneId: 'flutter-interstitial-demo',
sceneDescription: 'Flutter 新插屏示例',
);
});
Future<void> _loadSplashAd() => _run(() async {
await _splashAd?.dispose();
final ad = SigmobSplashAd(
request: SigmobAdRequest(
placementId: _splashPlacementController.text,
userId: 'flutter-demo-user',
),
fetchDelaySeconds: 5,
);
_splashAd = ad;
await ad.load();
if (mounted) setState(() => _status = '开屏广告加载请求已提交');
});
Future<void> _showSplashAd() => _run(() async {
final ad = _splashAd;
if (ad == null) {
setState(() => _status = '请先加载开屏广告');
return;
}
if (!await ad.isReady) {
setState(() => _status = '开屏广告尚未 Ready');
return;
}
await ad.show();
});
@override
void dispose() {
_eventSubscription?.cancel();
_appIdController.dispose();
_appKeyController.dispose();
_rewardedPlacementController.dispose();
_interstitialPlacementController.dispose();
_splashPlacementController.dispose();
_rewardedAd?.dispose();
_interstitialAd?.dispose();
_splashAd?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('flutter_sigmob')),
body: ListView(
padding: const EdgeInsets.all(24),
children: [
TextField(
controller: _appIdController,
decoration: const InputDecoration(labelText: 'Sigmob App ID'),
),
const SizedBox(height: 12),
TextField(
controller: _appKeyController,
decoration: const InputDecoration(labelText: 'Sigmob App Key'),
),
const SizedBox(height: 24),
Text(_status, textAlign: TextAlign.center),
const SizedBox(height: 24),
FilledButton(
onPressed: _busy ? null : _readSdkVersion,
child: const Text('读取 SDK 版本'),
),
const SizedBox(height: 12),
FilledButton(
onPressed: _busy ? null : _initialize,
child: const Text('初始化 SDK'),
),
const SizedBox(height: 12),
FilledButton(
onPressed: _busy ? null : _start,
child: const Text('启动 SDK'),
),
const Divider(height: 40),
TextField(
controller: _rewardedPlacementController,
decoration: const InputDecoration(labelText: '激励视频广告位 ID'),
),
const SizedBox(height: 16),
FilledButton(
onPressed: _busy ? null : _loadRewardedAd,
child: const Text('加载激励视频'),
),
const SizedBox(height: 12),
FilledButton(
onPressed: _busy ? null : _showRewardedAd,
child: const Text('展示激励视频'),
),
const Divider(height: 40),
TextField(
controller: _interstitialPlacementController,
decoration: const InputDecoration(labelText: '新插屏广告位 ID'),
),
const SizedBox(height: 16),
FilledButton(
onPressed: _busy ? null : _loadInterstitialAd,
child: const Text('加载新插屏'),
),
const SizedBox(height: 12),
FilledButton(
onPressed: _busy ? null : _showInterstitialAd,
child: const Text('展示新插屏'),
),
const Divider(height: 40),
TextField(
controller: _splashPlacementController,
decoration: const InputDecoration(labelText: '开屏广告位 ID'),
),
const SizedBox(height: 16),
FilledButton(
onPressed: _busy ? null : _loadSplashAd,
child: const Text('加载开屏广告'),
),
const SizedBox(height: 12),
FilledButton(
onPressed: _busy ? null : _showSplashAd,
child: const Text('展示开屏广告'),
),
],
),
);
}
}