2024-10-16 10:52:08 +08:00

64 lines
1.6 KiB
Dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:union_ad_ssgf/union_ad_ssgf.dart';
import 'package:union_ad_ssgf/event/ad_error_event.dart';
import 'package:union_ad_ssgf/event/ad_reward_event.dart';
import 'package:union_ad_ssgf_example/ads_config.dart';
import 'package:union_ad_ssgf_example/home_page.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
setAdEvent();
init().then((value) {
if (value) {
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
/// 初始化广告 SDK
Future<bool> init() async {
bool result = await UnionAdSsgf.initAd(AdsConfig.appId);
print("----->> result: $result");
debugPrint("广告SDK 初始化${result ? '成功' : '失败'}");
return result;
}
/// 设置广告监听
Future<void> setAdEvent() async {
UnionAdSsgf.onEventListener((event) {
debugPrint('adId:${event.adId} action:${event.action}');
if (event is AdErrorEvent) {
// 错误事件
debugPrint(' errCode:${event.errCode} errMsg:${event.errMsg}');
} else if (event is AdRewardEvent) {
// 激励事件
debugPrint(
' transId:${event.transId} userId:${event.userId} customData:${event.customData}');
}
});
}
}